8000 Update Ch 8 solutions · rakash/python-basics-exercises@e620f4f · GitHub
[go: up one dir, main page]

Skip to content

Commit e620f4f

Browse files
committed
Update Ch 8 solutions
1 parent 9a1110f commit e620f4f

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def roll():
1414

1515
# Exercise 2
1616
# Simulate 10,000 rolls of a die and display the average number rolled.
17-
trials = 10000
17+
num_rolls = 10_000
1818
total = 0
1919

20-
for trial in range(0, trials):
20+
for trial in range(num_rolls):
2121
total = total + roll()
2222

23-
avg_roll = total / trials
23+
avg_roll = total / num_rolls
2424

25-
print(f"The average result of {trials} throws is {avg_roll}")
25+
print(f"The average result of {num_rolls} rolls is {avg_roll}")

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

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,44 @@
1212
# 4. After the first toss, you'll need another loop to keep flipping while you
1313
# get the same result as the first flip.
1414

15-
from random import randint
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+
1625

1726
flips = 0
18-
trials = 10000
19-
20-
for trial in range(0, trials):
21-
flips += 1 # first flip
22-
if randint(0, 1) == 0: # flipped tails on first flip
23-
while randint 10000 (0, 1) == 0: # keep flipping tails
24-
flips += 1
25-
flips += 1 # finally flipped heads
26-
else: # otherwise, flipped heads on first flip
27-
while randint(0, 1) == 1: # keep flipping heads
28-
flips += 1
29-
flips += 1 # finally flipped tails
30-
31-
print(flips / trials)
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}.")

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,31 @@
1212
# 4. After the first toss, you'll need another loop to keep flipping while you
1313
# get the same result as the first flip.
1414

15-
from random import randint
15+
import random
1616

1717

18-
trials = 100_000
19-
flips = 0
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"
2024

21-
for trial in range(1, trials):
22-
first_flip = randint(0, 1)
23-
flips += 1
24-
while randint(0, 1) == first_flip:
25-
flips += 1
26-
flips += 1
2725

28-
print(f"The average number of coin flips was {flips / trials}")
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}.")

0 commit comments

Comments
 (0)
0