Harvard:Biophysics 101/Notebook:ZS/2007-2-8
From OpenWetWare
Assignment 2
Code
#!/usr/bin/env python #Assignment 2, Zachary Sun (zsun@fas) from random import * hcount = 0 tcount = 0 string = [] y = [] w = [] u = '' v = '' for t in range(10): # creates a list of repetitive T's u = u + "T" y.append(u) for t in range(10): # creates a list of repetitive T's v = v + "H" w.append(v) for i in range(10000): # create the random coin tosses and strings flip = '' for z in range(10): if random() < 0.5: flip = flip + "H" hcount = hcount + 1 else: flip = flip + "T" tcount = tcount + 1 string.append(flip) for z in y: # counting continuous stretches of T countStretch = 0 for i in range(len(string)): countStretch = countStretch + string[i].count(z) print z,':',countStretch print '\n' for z in w: # counting continuous stretches of H countStretch = 0 for i in range(len(string)): countStretch = countStretch + string[i].count(z) print z,':',countStretch
Output
T : 49899 TT : 15491 TTT : 6093 TTTT : 2581 TTTTT : 1076 TTTTTT : 471 TTTTTTT : 194 TTTTTTTT : 73 TTTTTTTTT : 29 TTTTTTTTTT : 7 H : 50101 HH : 15652 HHH : 6152 HHHH : 2619 HHHHH : 1131 HHHHHH : 486 HHHHHHH : 183 HHHHHHHH : 76 HHHHHHHHH : 29 HHHHHHHHHH : 5