commit 97d8e06b081b4c1fce9762b4d8a294b31e8a1663 Author: webartifex Date: Tue Mar 28 01:42:36 2017 +0200 Add solution to rolling dice problem diff --git a/rolling_dice.py b/rolling_dice.py new file mode 100644 index 0000000..78c140d --- /dev/null +++ b/rolling_dice.py @@ -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