Harvard:Biophysics 101/2007/02/08: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
Line 34: Line 34:
     print '%2s: %5d' % (i, tally[i]) # print the results
     print '%2s: %5d' % (i, tally[i]) # print the results
</pre>
</pre>
*Example output
  2: 45129
  3: 20055
  4:  8776
  5:  3784
  6:  1588
  7:  638
  8:  247
  9:    81
10:    22

Revision as of 10:29, 8 February 2007

Solution to Feb 6 Assignment

  • Here's my take on the Feb 6 assignment. --smd 12:28, 8 February 2007 (EST)
#!/usr/bin/env python

import random

# create a list to store our results
tally = [0 for i in range(11)]

for i in range(10000):
    # generate a random 10-mer
    coinflip = ''.join([random.choice('HT') for j in range(10)])

    # tally k-mers in the 10-mer
    for k in range(2,11):
        Hcount = Tcount = 0

        H = ''.join(['H' for n in range(k)]) # k-mer of H's
        pos = coinflip.find(H,0)
        while not pos == -1:
            Hcount = Hcount + 1
            pos = coinflip.find(H,pos+1)

        T = ''.join(['T' for n in range(k)]) # k-mer of T's
        pos = coinflip.find(T,0)
        while not pos == -1:
            Tcount = Tcount + 1
            pos = coinflip.find(T,pos+1)

        tally[k] = tally[k] + Hcount + Tcount

for i in range(2,11):
    print '%2s: %5d' % (i, tally[i]) # print the results
  • Example output
 2: 45129
 3: 20055
 4:  8776
 5:  3784
 6:  1588
 7:   638
 8:   247
 9:    81
10:    22