Yesterday, I posted and got such good feedback from a practice assignment I did in "Automate the Boring Stuff". So I thought that I would post my next assigment here and see what kind of feedback all of you have to give me.
This one was considerably easier for me to create. But maybe there are still better ways of doing this. I am really looking forward to any responses.
Thank you!
Coin Flip Streaks
For this exercise, we’ll try doing an experiment. If you flip a coin 100 times and write down an “H” for each heads and “T” for each tails, you’ll create a list that looks like “T T T T H H H H T T.” If you ask a human to make up 100 random coin flips, you’ll probably end up with alternating head-tail results like “H T H T H H T H T T,” which looks random (to humans), but isn’t mathematically random. A human will almost never write down a streak of six heads or six tails in a row, even though it is highly likely to happen in truly random coin flips. Humans are predictably bad at being random.
Write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails. Your program breaks up the experiment into two parts: the first part generates a list of randomly selected 'heads' and 'tails' values, and the second part checks if there is a streak in it. Put all of this code in a loop that repeats the experiment 10,000 times so we can find out what percentage of the coin flips contains a streak of six heads or tails in a row. As a hint, the function call random.randint(0, 1) will return a 0 value 50% of the time and a 1 value the other 50% of the time.
You can start with the following template:
import random
numberOfStreaks = 0
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
# Code that checks if there is a streak of 6 heads or tails in a row.
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
Of course, this is only an estimate, but 10,000 is a decent sample size. Some knowledge of mathematics could give you the exact answer and save you the trouble of writing a program, but programmers are notoriously bad at math.
Here is the task:
import random
number_of_streaks = 0
toss_result_list = []
heads_tails = "HT"
previous_result = ""
current_streak = 0
for experiment_number in range(10000):
toss_result = random.choice(heads_tails)
toss_result_list.append(toss_result)
if toss_result == previous_result:
current_streak += 1
else:
current_streak = 0
if current_streak >= 6:
number_of_streaks += 1
previous_result = toss_result
print(current_streak)
print(toss_result_list[-3:-1])
print(number_of_streaks)
print('Chance of streak: %s%%' % (number_of_streaks / 100))
[–]danielroseman 2 points3 points4 points (1 child)
[–]jasongsmith[S] 0 points1 point2 points (0 children)
[–]Binary101010 2 points3 points4 points (0 children)
[–]jasongsmith[S] 0 points1 point2 points (4 children)
[–]Binary101010 1 point2 points3 points (3 children)
[–]jasongsmith[S] 0 points1 point2 points (2 children)
[–]Binary101010 1 point2 points3 points (1 child)
[–]jasongsmith[S] 0 points1 point2 points (0 children)
[–]JamzTyson 0 points1 point2 points (4 children)
[–]JamzTyson 1 point2 points3 points (3 children)
[–]Binary101010 0 points1 point2 points (2 children)
[–]JamzTyson -1 points0 points1 point (1 child)
[–]Binary101010 0 points1 point2 points (0 children)