Add solution to rolling dice problem
This commit is contained in:
commit
97d8e06b08
1 changed files with 37 additions and 0 deletions
37
rolling_dice.py
Normal file
37
rolling_dice.py
Normal file
|
@ -0,0 +1,37 @@
|
|||
"""
|
||||
rolling_dice.py
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
This module solves the rolling dice in-class problem.
|
||||
|
||||
Run with "python rolling_dice.py"
|
||||
"""
|
||||
|
||||
|
||||
def average(i, outcomes):
|
||||
rsp = 0
|
||||
for j in range(1, 7):
|
||||
rsp += outcomes.get((i, j), 0)
|
||||
return rsp / 6
|
||||
|
||||
|
||||
def simulate(n_rounds):
|
||||
outcomes = {}
|
||||
for i in reversed(range(1, n_rounds + 1)):
|
||||
for j in range(1, 7):
|
||||
outcomes[(i, j)] = max(j, average(i + 1, outcomes))
|
||||
return outcomes
|
||||
|
||||
|
||||
i = 0
|
||||
while True:
|
||||
i += 1
|
||||
outcomes = simulate(i)
|
||||
below_5 = False
|
||||
for j in range(1, 7):
|
||||
if outcomes[(1, j)] < 5:
|
||||
below_5 = True
|
||||
|
||||
if not below_5:
|
||||
print('rounds needed: {}'.format(i))
|
||||
break
|
Loading…
Reference in a new issue