python - Determining Longest run of Heads and Tails -
i have question fourth function, longestrun
. want output longest run of heads , longest run of tails based on how many flips (n) user enters. have tried ton of different things, , doesn't seem work. can guys me out?:
def longestrun(n): h = 0 t = 1 mylist = [] in range(n): random.randint(0,1) if random.randint(0,1) == h: mylist.append('h') else: mylist.append('t')
i want next piece output 2 things.
"the longest run of heads was: " , whatever longest run of heads was.
"the longest run of tails was: " , whatever longest run of tails was.
please me! thank guys!
from itertools import groupby my_list = [1,1,0,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0,1] max(len(list(v)) k,v in groupby(my_list) if k==1)
is fun way group consecutive values , counts longest length of 1's, if use "h/t" instead change if condition @ end
Comments
Post a Comment