Random_String_Exercise

3793 days ago by alex

randrange? 
       
chr? 
       
[chr(i) for i in range(65,91)] 
       
# Generate a string of 250 random characters s = [] for i in range(250): s.append(chr(randrange(65,91))) s 
       
s[1:4] 
       
# Look for a repeated trigram trigrams = [] for i in range(248): trigram = s[i:i+3] if trigram in trigrams: print "Duplicate trigram", s[i:i+3], "at", i else: trigrams.append(trigram) 
       
#################################################### # # Try 50 iterations -- # Generate a random alphabetic string of length 250 # Look for a trigram that appears more than once # #################################################### # for k in range(50): s = [] for i in range(250): s.append(chr(randrange(65,91))) trigrams = [] for i in range(248): trigram = s[i:i+3] if trigram in trigrams: print "Duplicate trigram %s at %3d, k = %3d" % (s[i:i+3], i, k) else: trigrams.append(trigram)