TChan/Notebook/2007-2-8

From OpenWetWare

Jump to: navigation, search

BP 101: Script 1

Assignment:

  • Write a python script that generates 10,000 strings of 10 random coinflips (H or T) and outputs the tally of continguous (overlapping) stretches of 2, 3, 4, 5, 6, 7, 8, 9, and 10 H's or T's in that set of 10,000 10-mers.
  • Post your code to your personal calendar entry for Feb 8, along with the output when it is run.
#!/usr/bin/python

import sys
import random

head_tail_set = ['H', 'T']

contiguous_Hs = [0,0,0,0,0,0,0,0,0,0]
contiguous_Ts = [0,0,0,0,0,0,0,0,0,0]

max_repeat = 10


for i in range(10000):
    flip_unit = ''
    for j in range(10):
        H_or_T = random.choice(head_tail_set)
        flip_unit = flip_unit + H_or_T
    for k in range(max_repeat):
        substr_H = ''.join(['H' for n in range(k+1)])
        substr_T = ''.join(['T' for p in range(k+1)])
        count_H = 0
        count_T = 0
        pos_H = flip_unit.find(substr_H,0)
        pos_T = flip_unit.find(substr_T,0)
        while not pos_H == -1:
            count_H = count_H + 1
            pos_H = flip_unit.find(substr_H,pos_H+1)
        while not pos_T == -1:
            count_T = count_T + 1
            pos_T = flip_unit.find(substr_T,pos_T+1)
        contiguous_Hs[(len(substr_H)-1)] = contiguous_Hs[(len(substr_H)-1)] + count_H
        contiguous_Ts[(len(substr_T)-1)] = contiguous_Ts[(len(substr_T)-1)] + count_T

print 'Contiguous Hs:'
for a in range(len(contiguous_Hs)-1):
    num_of_Hs = ('H'*a) + 'H'
    print '%12s %d' % (num_of_Hs, contiguous_Hs[len(num_of_Hs)])

print '\n'
                                    
print 'Contiguous Ts:'
for b in range(len(contiguous_Ts)-1):
    num_of_Ts = ('T'*b) + 'T'
    print  '%12s %d' % (num_of_Ts, contiguous_Ts[len(num_of_Ts)])
                                    


Contiguous Hs:
           H 22780
          HH 10285
         HHH 4629
        HHHH 2047
       HHHHH 839
      HHHHHH 343
     HHHHHHH 133
    HHHHHHHH 45
   HHHHHHHHH 11


Contiguous Ts:
           T 22314
          TT 9909
         TTT 4369
        TTTT 1873
       TTTTT 771
      TTTTTT 303
     TTTTTTT 110
    TTTTTTTT 38
   TTTTTTTTT 5
Personal tools