Harvard:Biophysics 101/2007/Notebook:Resmi Charalel/2007-2-8: Difference between revisions

From OpenWetWare
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
== Script ==
== Script ==


#!/usr/bin/env python
  #!/usr/bin/env python


from random import *
  from random import *


mx=10
  mx=10
countH=[0,0,0,0,0,0,0,0,0]
  countH=[0,0,0,0,0,0,0,0,0]
countT=[0,0,0,0,0,0,0,0,0]
  countT=[0,0,0,0,0,0,0,0,0]


#Generation of list of 10,000 strings of 10 random coinflips and tally of stretches of H's and T's
  #Generation of list of 10,000 strings of 10 random coinflips and tally of stretches of H's and T's
for x in range(10000):
  for x in range(10000):
    s=[]
      s=[]
    for i in range(mx):
      for i in range(mx):
        random = choice(range(0,2,1))
          random = choice(range(0,2,1))


        if random==0:
          if random==0:
            s.append('H')
              s.append('H')


        if random==1:
          if random==1:
            s.append('T')
              s.append('T')
        substr=''.join(s)
          substr=''.join(s)
       
      for i in range(9):
          substH = ''.join(['H' for n in range(i+2)])
          pos = substr.find(substH,0)
          
          
    for i in range(9):
          while not pos == -1:
        substH = ''.join(['H' for n in range(i+2)])
              countH[i] = countH[i] + 1
        pos = substr.find(substH,0)
              pos = substr.find(substH,pos+1)
       
        while not pos == -1:
            countH[i] = countH[i] + 1
            pos = substr.find(substH,pos+1)
                  
                  
    for i in range(9):
      for i in range(9):
        substT = ''.join(['T' for n in range(i+2)])
          substT = ''.join(['T' for n in range(i+2)])
      
      
        pos = substr.find(substT,0)
          pos = substr.find(substT,0)
        while not pos == -1:
          while not pos == -1:
            countT[i] = countT[i] + 1
              countT[i] = countT[i] + 1
            pos = substr.find(substT,pos+1)
              pos = substr.find(substT,pos+1)


print "10,000 strings of 10 random coinflips were generated.  The following tallies of H's and T's were recorded."  
  print "10,000 strings of 10 random coinflips were generated.  The following tallies of H's and T's were recorded."  
print "Occurence(s) of HH:", countH[0]
  print "Occurence(s) of HH:", countH[0]
print "Occurence(s) of HHH:", countH[1]
  print "Occurence(s) of HHH:", countH[1]
print "Occurence(s) of HHHH:", countH[2]
  print "Occurence(s) of HHHH:", countH[2]
print "Occurence(s) of HHHHH:", countH[3]
  print "Occurence(s) of HHHHH:", countH[3]
print "Occurence(s) of HHHHHH:", countH[4]
  print "Occurence(s) of HHHHHH:", countH[4]
print "Occurence(s) of HHHHHHH:", countH[5]
  print "Occurence(s) of HHHHHHH:", countH[5]
print "Occurence(s) of HHHHHHHH:", countH[6]
  print "Occurence(s) of HHHHHHHH:", countH[6]
print "Occurence(s) of HHHHHHHHH:", countH[7]
  print "Occurence(s) of HHHHHHHHH:", countH[7]
print "Occurence(s) of HHHHHHHHHH:", countH[8]
  print "Occurence(s) of HHHHHHHHHH:", countH[8]
print "Occurence(s) of TT:", countT[0]
  print "Occurence(s) of TT:", countT[0]
print "Occurence(s) of TTT:", countT[1]
  print "Occurence(s) of TTT:", countT[1]
print "Occurence(s) of TTTT:", countT[2]
  print "Occurence(s) of TTTT:", countT[2]
print "Occurence(s) of TTTTT:", countT[3]
  print "Occurence(s) of TTTTT:", countT[3]
print "Occurence(s) of TTTTTT:", countT[4]
  print "Occurence(s) of TTTTTT:", countT[4]
print "Occurence(s) of TTTTTTT:", countT[5]
  print "Occurence(s) of TTTTTTT:", countT[5]
print "Occurence(s) of TTTTTTTT:", countT[6]
  print "Occurence(s) of TTTTTTTT:", countT[6]
print "Occurence(s) of TTTTTTTTT:", countT[7]
  print "Occurence(s) of TTTTTTTTT:", countT[7]
print "Occurence(s) of TTTTTTTTTT:", countT[8]
  print "Occurence(s) of TTTTTTTTTT:", countT[8]

Revision as of 22:33, 11 February 2007

Script

 #!/usr/bin/env python
 from random import *
 mx=10
 countH=[0,0,0,0,0,0,0,0,0]
 countT=[0,0,0,0,0,0,0,0,0]
 #Generation of list of 10,000 strings of 10 random coinflips and tally of stretches of H's and T's
 for x in range(10000):
     s=[]
     for i in range(mx):
         random = choice(range(0,2,1))
         if random==0:
             s.append('H')
         if random==1:
             s.append('T')
         substr=.join(s)
        
     for i in range(9):
         substH = .join(['H' for n in range(i+2)])
         pos = substr.find(substH,0)
       
         while not pos == -1:
             countH[i] = countH[i] + 1
             pos = substr.find(substH,pos+1)
               
     for i in range(9):
         substT = .join(['T' for n in range(i+2)])
   
         pos = substr.find(substT,0)
         while not pos == -1:
             countT[i] = countT[i] + 1
             pos = substr.find(substT,pos+1)
 print "10,000 strings of 10 random coinflips were generated.  The following tallies of H's and T's were recorded." 
 print "Occurence(s) of HH:", countH[0]
 print "Occurence(s) of HHH:", countH[1]
 print "Occurence(s) of HHHH:", countH[2]
 print "Occurence(s) of HHHHH:", countH[3]
 print "Occurence(s) of HHHHHH:", countH[4]
 print "Occurence(s) of HHHHHHH:", countH[5]
 print "Occurence(s) of HHHHHHHH:", countH[6]
 print "Occurence(s) of HHHHHHHHH:", countH[7]
 print "Occurence(s) of HHHHHHHHHH:", countH[8]
 print "Occurence(s) of TT:", countT[0]
 print "Occurence(s) of TTT:", countT[1]
 print "Occurence(s) of TTTT:", countT[2]
 print "Occurence(s) of TTTTT:", countT[3]
 print "Occurence(s) of TTTTTT:", countT[4]
 print "Occurence(s) of TTTTTTT:", countT[5]
 print "Occurence(s) of TTTTTTTT:", countT[6]
 print "Occurence(s) of TTTTTTTTT:", countT[7]
 print "Occurence(s) of TTTTTTTTTT:", countT[8]