# [Community Puzzle] Dice probability calculator

Hi

I got the notification about this community puzzle (https://www.codingame.com/training/medium/dice-probability-calculator) and got interested. It’s a nice idea, and the “puzzle” part is mainly the solution implementation strategy (some interesting options exist).

However, I got stuck on validation case 5.
The probability of several of the results is 3.125% (1 out of 32 cases), and the requirement call for rounding the result to 2 places after the decimal point. So my program output was 3.13, which is indeed the correct rounding.

But the validator expects 3.12 ???

I hardcoded this case into my code, just to view other solutions, and it seems to me that other solutions also output 3.13…

Would appreciate any insight into this It’s one correct rounding, but unfortunately multiple could make sense and the puzzle doesn’t specify. Out of curiosity, what language and rounding method are you using?

About validation case 5 “Subtraction of dice”, it doesn’t have an odd multiple of 0.005 as an unrounded solution, so you shouldn’t have that problem.

I’ll see if I can eliminate the others I can find. Done. It was only test 5. No validator was impacted.

2 Likes

I’m using Javascript, and tried several combination of Math.round() and toFixed(). All output the (mathematically) correct answer of 3.13

In test case 5, there are four distinct values that result in probability of 3.125: -7, -6, 5, 6. Each appears exactly once out of the 32 possible dice combinations (1/32 = 3.125%)

EDIT - regarding the rounding:
The rules specify “Floats are formatted with 2 decimal figures, rounded”
The mathematical rule for rounding is

• If the number you are rounding is followed by 5, 6, 7, 8, or 9, round the number up
• If the number you are rounding is followed by 0, 1, 2, 3, or 4, round the number down

As far as I know, this is a universal definition, not a language-specific implementation.

EDIT 2 - rounding can indeed be a rabbit hole
https://www.mathsisfun.com/numbers/rounding-methods.html

I just saw that test case 5 was changed to avoid this problem. Thanks !

(original expression was “d8-2d4", it is now "d9-2d4”)

LOL. Good for you, you edited before I got back to reading.

Yup, I did that. I notified in the problem’s backroom comment box, but forgot to come back and announce it here.

I’m surprised this one didn’t work, but indeed the author (and most of us AFAIK) used printf formatting, which rounds without specifying too much either; and the MDN docs do mention JS Math.round differs from common languages.

In any case, this wasn’t a problem about rounding, so good riddance.

1 Like

Hello,

I an coding in JAVA.

I can’t get “FIX IT” test, I know that my code is not optimized but I don’t know what. Note that sometime the test is OK but when I submit, it failed at “FIX IT”.

I am using a recursive function to interprete d’n’ (with Pattern/Matcher) in order to extract all possibles outputs (note that I also compute propability simply by dividing by the number n. (Everything work perfectly).

I am using TreeMap in order to automatic sort of output values.

What have i missed? Any hint in order to optimize my code?

Thanks,

If you solved this medium puzzle, I recommend to also do the hard puzzle Order of Oopserations by investing another 2 minutes.
(Actually the ‘hard’ one is the simpler one - no dice, no parenthesis, just a simple formula with custom operator precedence.)

I am programming in C++ and don’t really know how I should begin this puzzle. I am rather a newbie in programming (started last year with a months-long break) and finished reading the book “Beginning C++ Through Game Programming” a few days ago. I guess I don’t have the practical thinking for solving this puzzle and am unsure if I should try solving this puzzle using all my problem solving power or if I should just start with an easier puzzle.
Everytime I start to think of a solution (or part of a solution), there is something that destroyes it. For example:
I started thinking of breaking the input string apart into substrings, evaluating each substring corresponding to the operator (or if there is a ‘d’ calculating the possible values and the probabilities and saving them in two arrays). “1+d4+1” -> look for ‘d’ -> there is a d4 -> through some algorithm calculate it to: [1, 2, 3, 4], [25.00, 25.00, 25.00, 25.00].
Now starts the problem. I don’t know how to go further now. I kinda extracted the “d4” and don’t know how my code will know, that “1+[my arrays?]+1” should use 1+[my arrays] to add 1 to the possible values in my corresponding array. I think to be more specific: I’m unsure how to “save” my array into that “1+d4+1” string.

I appreciate any help To go further than where you were stuck, you should try solving it without a computer, without writing any program. Just have your pencil and paper, may be with a calculator, to find out the answer. This is a simple math problem.

Write down the detailed steps and logic of solving the problem. This is a significant progress you may not realize how important it is. The rest of the work is simple - use your programming language to simulate your manual effort.

Solving a problem and writing the solution into a program are two distinct steps. Handle them separately. Often the first part is much harder.

If you are not quite confident with loops, conditions, array, etc it is better to try easier problems first.

I’d say I know how to use loops, conditions, arrays etc. and I already wrote multiple pages on paper of how I would solve it without code and how to implement that into code (at least tried). I always write pseudocode first and that’s where I get stuck. I haven’t written any real code yet for this puzzle.
Maybe my approach to solve this in code is wrong and that is why I get stuck.

I think I will try some other puzzles first and learn more about algorithms, data structures etc.

Trying to pull you off the pool of mud where you were stuck, here is the example with explanation.

You already know there is an array [1,2,3,4] obtained from d4
You know how to put each element into a function
1 + arr + 1 gets 3, etc

That means you wish to have a function like this:
int calculate(int n)

However, this function should not be hard coded with 1+n+1. It is composed of multiple Terms and multiple Operators, both are variables from the input.
So your function should include these variables to make it flexible:

int calculate(terms[], ops[], int n)
terms are [1, d4, 1]
ops are [+, +]

Inside the function, you will replace d4 with n.
Your function is to bring these elements together to obtain output 3 when n is 1.

I did not include the possibility of having brackets in the function. Skip it for now. It is hard.

I did not precisely tell you what datatype should be used. You should invent your own way.

Though I said you need a function, it can also be a class containing functions and data structures.

If you can build such a function or class, you should be able to pass most test cases.

2 Likes

I have a problem that I don’t know how to approach. My code seems to parse expression properly. I have a problem with the distribution results
I calculate phrases multiple times (10,000 iterations) to get a batch and divide by total. My Python code:

Where do I go wrong? Any tips??

Maybe 10,000 iterations is not enough to get a precise-enough answer, or maybe some bad random is at play.

Anyway, I suggest dropping this evaluation-through-XXX-iterations stuff, and trying to compute the exact probability distribution (“import fractions” might help).

2 Likes

I am able to pass all of the testcases, but I fail on the final validator. Any help would be greatly appreciated.

I tried that one out and got the same results, HAHA! I can pass all the testcases, but I fail validator 4. 