All problems
Expected Longest Run (Seeded)
Problem
Estimate the expected length of the longest run of heads in n fair coin flips by simulation. Run trials experiments with random.Random(seed) and return the average longest run. Return 0.0 for n <= 0 or trials <= 0.
Hint
Track the current run and the best run for each simulated sequence.
Test cases
case 1
in: [10,2000,42]
expected: 2.75
case 2
in: [20,1000,7]
expected: 3.742
case 3
in: [0,5,1]
expected: 0
case 4
in: [1,100,0]
expected: 0.37
Starter codePython
import random
def expected_longest_run_simulation(n, trials, seed):
# Your code here
pass4 test cases · run your code against them in the editor