1
0
Fork 0

Add solution to rolling dice problem

This commit is contained in:
Alexander Hess 2017-03-28 01:42:36 +02:00
commit 97d8e06b08

37
rolling_dice.py Normal file
View 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