Back
Close

Dice probability calculator

Statement

 Goal

Each Friday, you play the famous 'Fix your Dungeon' RPG with friends. You'd like to optimize your character: you need to know the probability of success of each test involving dice throws.

Your program takes in input an expression (possibly with dice throws) and must print all possible outcomes along with their probabilities.

The expression is an arithmetic expression with parentheses and the following operators, from highest to lowest precedence:
* multiplication
+ and - addition and subtraction
> greater-than comparison: evaluates to 1 if true, 0 if false

The operands are one of:
n: a decimal positive integer
dn: a 'd' followed by a strict positive number, representing a die throw from 1 to n by a uniform distribution

Examples of expressions:
3*2+5 evaluates to 11
d6: evaluates to an integer from 1 to 6, uniform
d6+d6: represents a double-dice throw
5>2: evaluates to 1
Input
A unique line containing the input expression.
The expression contains no spaces.
Output
N lines: an integer (outcome), a space and a float (percent probability of this outcome)

Outcomes are sorted in ascending order.
Floats are formatted with 2 decimal figures, rounded.
Constraints
The inputs are all valid expressions: no error handling is needed.
Maximum length of expression: 100 chars
Example
Input
1+d4+1
Output
3 25.00
4 25.00
5 25.00
6 25.00

Tags
Probability

Difficulty
Medium

Test cases
A die and two additions Test
Input
1+d4+1
Output
3 25.00 4 25.00 5 25.00 6 25.00

A die and two additions Validator
Input
1+d5+2
Output
4 20.00 5 20.00 6 20.00 7 20.00 8 20.00

Simple Comparison Test
Input
(2>5)+2*(5>2)+4*(10>5)
Output
6 100.00

Simple Comparison Validator
Input
(10>4)+2*(9>2)+4*(5>8)
Output
3 100.00

Addition of two dice Test
Input
d6+d6
Output
2 2.78 3 5.56 4 8.33 5 11.11 6 13.89 7 16.67 8 13.89 9 11.11 10 8.33 11 5.56 12 2.78

Addition of two dice Validator
Input
d4+d6
Output
2 4.17 3 8.33 4 12.50 5 16.67 6 16.67 7 16.67 8 12.50 9 8.33 10 4.17

Comparaison with sum of dice Test
Input
8>d6+d6
Output
0 41.67 1 58.33

Comparaison with sum of dice Validator
Input
5>d4+d6
Output
0 75.00 1 25.00

Subtraction of dice Test
Input
d9-2*d4
Output
-7 2.78 -6 2.78 -5 5.56 -4 5.56 -3 8.33 -2 8.33 -1 11.11 0 11.11 1 11.11 2 8.33 3 8.33 4 5.56 5 5.56 6 2.78 7 2.78

Subtraction of dice Validator
Input
d6-d6
Output
-5 2.78 -4 5.56 -3 8.33 -2 11.11 -1 13.89 0 16.67 1 13.89 2 11.11 3 8.33 4 5.56 5 2.78

Multiplication of dice Test
Input
12-d2*d6
Output
0 8.33 2 8.33 4 8.33 6 16.67 7 8.33 8 16.67 9 8.33 10 16.67 11 8.33

Multiplication of dice Validator
Input
10-d5*d2
Output
0 10.00 2 10.00 4 10.00 5 10.00 6 20.00 7 10.00 8 20.00 9 10.00

FIX IT ! Test
Input
2*d6*d3-(3+d3)*d5>0
Output
0 59.63 1 40.37

FIX IT ! Validator
Input
2*d5*d3-(3+d4)*d6>0
Output
0 73.33 1 26.67

Solution language

Solution

Stub generator input