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…
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.
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?
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.
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.
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: