| Hosted by CoCalc | Download
print 'sss\'sss'
sss'sss
u'8220
Error in lines 1-1 Traceback (most recent call last): File "/projects/sage/sage-7.5/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 982, in execute exec compile(block+'\n', '', 'single') in namespace, locals File "<string>", line 1 u'8220 ^ SyntaxError: EOL while scanning string literal
data = ["H", "T", "H", "H"]
def number_of_heads(L): count = 0 for i in L: if i == "H": count = count + 1 return count
number_of_heads(data)
3
def coin_flips(n): flips = [] for i in [0..(n-1)]: if random() < .5: flips.append("H") else: flips.append("T") return flips
coin_flips(5)
['T', 'T', 'H', 'H', 'H']
def coin_flips(n): flips = [] for i in [0..(n-1)]: if random() < .5: flips.append("H") else: flips.append("T") return flips def longest_run_of_heads(n): flip_results = coin_flips(n) print flip_results last = flip_results[0] count = 0 if last == "H": count = 1 longest = 1 for i in [1..(n-1)]: current = flip_results[i] if current == "H" and last == "T": count = 1 elif current == "H" and last == "H": count = count + 1 elif current == "T" and last == "H": #current streak of heads is over if count > longest: #every time a streak ends, we check if its longer than the previous streaks longest = count count = 1 #if current = "T" and last = "T": don't do anything return longest
longest_run_of_heads(7)
['H', 'T', 'T', 'H', 'H', 'H', 'T'] 4
def coin_flips(n): flips = [] for i in [0..(n-1)]: if random() < .5: flips.append("H") else: flips.append("T") return flips def longest_run_of_heads_or_tails(n): flip_results = coin_flips(n) print flip_results last = flip_results[0] #last records the last/previously looked at coin flip countH = 0 #records the length of the current streak of heads countT = 0 #records the length of the current streak of tails longestH = 0 #records the length of the longest streak of heads longestT = 0 #records the length of the longest streak of tails #this block records whether the initial flip is heads or tails if last == "H": countH = 1 longestH = 1 else: countT = 1 longestT = 1 for i in [1..(n-1)]: current = flip_results[i] #current is the currently looked-at flip in the list of coin flip results if current == "H" and last == "T": countH = 1 countT = 0 #the streak of T's is over last = "H" #because the currently looked-at flip becomes the last/previously looked-at flip elif current == "H" and last == "H": countH = countH + 1 elif current == "T" and last == "T": countT = countT + 1 elif current == "T" and last == "H": #current streak of heads is over countH = 0 countT = 1 last = "T" #because the currently looked-at flip becomes the last/previously looked-at flip if countT > longestT: #every time a streak ends, we check if its longer than the previous streaks longestT = countT if countH > longestH: longestH = countH print "The longest streak of heads is {}".format(longestH) print "The longest streak of tails is {}".format(longestT) return max(longestT, longestH)
longest_run_of_heads_or_tails(10)
['H', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'T', 'T'] The longest streak of heads is 5 The longest streak of tails is 3 5