You will write three small functions that count the ways to get a total with several dice: the number of ways for one total, the most likely total, and the chance of a total of at most some value. The checker uses two, three and four dice.
The idea. List every ordered outcome of the dice with itertools.product. A total is made in as many ways as there are outcomes with that sum. Use Fraction, so the probabilities stay exact.
Your tasks.
ways(dice, total). Return the number of ordered outcomes of dice dice whose sum is total.most_likely(dice). Return the smallest total that has the most ways.chance_at_most(dice, total). Return the probability that the total is at most total, as a Fraction.Have you tried it?
Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.
Log in to join the discussion.
Loading…