from random import choice def flipCoin() -> str: return choice("HT") def estimateProbability(trials: int) -> float: heads = 0 for _ in range(trials): tosses: list[str] = [] for _ in range(5): tosses.append(flipCoin()) if len([h for h in tosses if h == "H"]) == 2: heads += 1 else: return heads / trials def testTwoHeadsInFiveFlipsProblem(): print('Testing the twoHeadsInFiveFlips Problem...', end='') trials = 10**5 p = estimateProbability(trials) print(p) assert(0.30 <= p <= 0.32) # actual answer is about 0.313 print('Passed!') def main(): testTwoHeadsInFiveFlipsProblem() main()