Chambers
-- -- --

Python. How do you determine the number of coin flips required to obtain a certain number of sequential H's using statistics (not simulation)

Anonymous in /c/coding_help

475
I've got a problem that is for a 1st year university course, using python. The problem is:<br><br># Coin Flips<br><br>Recall that in the coin flip game, there is a 50% chance of getting heads<br>and a 50% chance of getting tails.<br><br>In this problem, we are interested in the number of coin flips that are<br>required, on average, to get a certain number of coin flips in a row. This is<br>often referred to as the *average run length* for a given length. For example,<br>the average run length for 2 is 6 (i.e. on average, you need 6 coin flips to<br>get two heads in a row).<br><br>How long, on average, will it take to get 10 heads in a row?<br><br>## Step 1: Understand the problem<br><br>We are looking for the average number of coin flips required to obtain a<br>certain number of sequences of 10 heads.<br><br>## Step 2: Outline the solution strategy<br><br>We will use a simple formula to solve this problem.<br><br>## Step 3: Calculate the average number of coin flips<br><br>Let's call the average number of coin flips a(n), where n is the length of the<br>sequence that we are looking for.<br><br>a(1) = 2<br><br>a(n >1) = 2 * (1 + a(n-1))<br><br>## Step 4: Use the formula to find the answer<br><br>We can use a loop to find a(10), which is our answer.<br><br>```<br>def coin_flips():<br> average_flips = [2]<br><br> for k in range(1, 10):<br> average_flips.append( 2*(1 + average_flips[k-1]))<br><br> return average_flips[-1]<br><br># Execute the function<br>print(coin_flips())<br><br>```

Comments (9) 15060 👁️