8000 Merge branch 'reorder-ch8-challenges' · MrDP18/python-basics-exercises@290ef39 · GitHub
[go: up one dir, main page]

Skip to content

Commit 290ef39

Browse files
committed
Merge branch 'reorder-ch8-challenges'
2 parents 3732d95 + 73de52b commit 290ef39

7 files changed

+110
-67
lines changed

ch08-conditional-logic/7-simulate-events-and-calculate-probabilities.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66

77

88
# Exercise 1
9-
# Simulate the toss of a die
10-
print(randint(1, 6))
9+
# Write a function that simulatee the roll of a die.
10+
def roll():
11+
"""Return random integer between 1 and 6"""
12+
return randint(1, 6)
1113

1214

1315
# Exercise 2
14-
# Simulate 10,000 throws of dice and display the average result
15-
trials = 10000
16+
# Simulate 10,000 rolls of a die and display the average number rolled.
17+
num_rolls = 10_000
1618
total = 0
17-
for trial in range(0, trials):
18-
total += randint(1, 6)
19-
avg_result = total / trials
20-
print(f"The average result of {trials} throws was {avg_result}")
19+
20+
for trial in range(num_rolls):
21+
total = total + roll()
22+
23+
avg_roll = total / num_rolls
24+
25+
print(f"The average result of {num_rolls} rolls is {avg_roll}")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 8.9 - Challenge: Simulate a Coin Toss Experiment
2+
# Solution to challenge
3+
4+
5+
# Simulate the results of a series of coin tosses and track the results
6+
7+
# This one is tricky to structure correctly. Try writing out the logic before
8+
# you start coding. Some additional pointers if you're stuck:
9+
# 1. You will need to use a `for` loop over a range of trials.
10+
# 2. For each trial, first you should check the outcome of the first flip.
11+
# 3. Make sure you add the first flip to the total number of flips.
12+
# 4. After the first toss, you'll need another loop to keep flipping while you
13+
# get the same result as the first flip.
14+
15+
import random
16+
17+
18+
def coin_flip():
19+
"""Randomly return 'heads' or 'tails'."""
20+
if random.randint(0, 1) == 0:
21+
return "heads"
22+
else:
23+
return "tails"
24+
25+
26+
flips = 0
27+
num_trials = 10_000
28+
29+
for trial in range(num_trials):
30+
if coin_flip() == "heads":
31+
# Increment the number of flips by 1
32+
flips = flips + 1
33+
while coin_flip() == "heads":
34+
# Keep incrementing the total number of flips
35+
# until "tails" is returned by coin_flip()
36+
flips = flips + 1
37+
# Once coin_flip() return "tails", the loop will exit,
38+
# but we need to add one more to flips to track the
39+
# last flip that generated "tails"
40+
flips = flips + 1
41+
else:
42+
# coin_flip() returned "tails" on the first flip.
43+
# Increment the number of flips by 1
44+
flips = flips + 1
45+
while coin_flip == "tails":
46+
# Keep incrementing the total number of flips
47+
# until "heads" is returned by coin_flip()
48+
flips = flips + 1
49+
# Once coin_flip() returns "heads", the loop will exit,
50+
# but we need to add one more to flips to track the
51+
# last flip that generated "heads"
52+
flips = flips + 1
53+
54+
avg_flips_per_trial = flips / num_trials
55+
print(f"The average number of flips per trial is {avg_flips_per_trial}.")
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 8.9 - Challenge: Simulate a Coin Toss Experiement
2+
# Alternative solution to challenge
3+
4+
5+
# Simulate the results of a series of coin tosses and track the results
6+
7+
# This one is tricky to structure correctly. Try writing out the logic before
8+
# you start coding. Some additional pointers if you're stuck:
9+
# 1. You will need to use a `for` loop over a range of trials.
10+
# 2. For each trial, first you should check the outcome of the first flip.
11+
# 3. Make sure you add the first flip to the total number of flips.
12+
# 4. After the first toss, you'll need another loop to keep flipping while you
13+
# get the same result as the first flip.
14+
15+
import random
16+
17+
18+
def coin_flip():
19+
"""Randomly return 'heads' or 'tails'."""
20+
if random.randint(0, 1) == 0:
21+
return "heads"
22+
else:
23+
return "tails"
24+
25+
26+
flips = 0
27+
num_trials = 10_000
28+
29+
for trial in range(num_trials):
30+
# Flip the coin once and increment the flips tally by 1
31+
first_flip = coin_flip()
32+
flips = flips + 1
33+
# Continue flipping the coin and updating the tally until
34+
# a different result is returned by coin_flips()
35+
while coin_flip() == first_flip:
36+
flips = flips + 1
37+
# Increment the flip tally once more to account for the
38+
# final flip with a different result
39+
flips = flips + 1
40+
41+
avg_flips_per_trial = flips / num_trials
42+
print(f"The average number of flips per trial is {avg_flips_per_trial}.")

ch08-conditional-logic/9a-challenge.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

ch08-conditional-logic/9b-challenge.py

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0