automate-boring-stuff/exercises/ex04/ex04b.py

41 lines
871 B
Python

import random
def flip(times):
res = []
for i in range(times):
res.append(random.choice(["H", "T"]))
return res
def check(flips, reps, streaks):
last, h, t = "", 0, 0
for f in flips:
if last == f:
if f == "H":
h += 1
else:
t += 1
else:
h = 0
t = 0
if h == reps or t == reps:
streaks += 1
break
last = f
return streaks
def test(times, length=100, reps=6):
streaks = 0
for i in range(times):
print(f"Testing {int((i * 100) / times)}%")
streaks = check(flip(length), reps, streaks)
print(f"Streak length: {reps}")
print(f"Flips length: {length}")
print(f"Tests: {times}")
print(f"Result: {streaks} streaks ({(streaks * 100) / times}%).")
test(10000, reps=6)