We're back with another code challenge, this one comes from user obrok on Codewars:
Create a simple calculator that given a string of operators (+ - * and /) and numbers separated by spaces returns the value of that expression
Example:
Calculator().evaluate("2 / 2 + 3 * 4 - 6") # => 7
Remember about the order of operations! Multiplications and divisions have a higher priority and should be performed left-to-right. Additions and subtractions have a lower priority and should also be performed left-to-right.
Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Top comments (25)
Alright, I'm gonna be the one who posts this.
If you add a check to verify the string is well formed, this will probably be the best and simplest answer.
Similarly unfair, the solution in R:
Warning: don't do this.
JavaScript
Live demo on CodePen
10 out of 10 challenges! :)
Although I was just able to make 2 using CSS only :-/
I'm gonna try to keep doing em each day in July if I can, you game lol?
I'm currently a day behind you since I didn't get this one done yesterday
Let's do it!
And, I see now that I don't handle non-whole-numbers, which the problem doesn't give me, but could come along anyway.
"5 / 4"
, for example.And that's entirely a regex problem.
Hrm.
Regex was
-?\d+
, which would handle whole numbers, but with division in the mix, you have to handle the possibility of decimals.The regex became
-?\d+(?:\.\d+)?
.(?:
indicates it's not matching, so we're not grabbing the fractional aspect independently,\.\d_
matches one dot and however many digits, and)?
closes the thing and matches only if found, so that it doesn't have to be a floating point number.Rust, verbosely:
Oh macro_rules are something I haven't dug into and explored much yet! Definitely used the built in ones, but haven't written any yet.
Thanks for the example!
I was intimidated by them for a while and avoided the topic - tried one once and now they even end up in my daily puzzle solutions :P Truly not complicated to use, and super handy!
Haskell:
Would anyone be able to help me abstract out that operation pattern? I'm a little stumped on how to de-duplicate this code, even though Haskell is great at that.
I recall this from Data Structures, and that there's ambiguity between handle from left and PEMDAS, which is a huge argument for Reverse Polish Notation.
For context (and an example I'm hitting in my testing),
4 - 6 - 2
. By PEMDAS, they're of equal order, but it really depends on if you want(4 - 6) - 2 == -4
or4 - (6 - 2) == 0
.With RPN, it's always
number number operator
, and variations on that are how we get precedence.4 6 2 - -
has6 2 -
innumber number operator
form and thus becomes4
, simplifying to4 4 -
and then0
.4 6 - 2 -
would get you the4 6 -
first, giving-2
, then-2 2 -
, which is -4`Not that I do my regular math like this...
Iβm (still) learning Erlang. This is my solution with the given operators and integer numbers. Iβm quite satisfied:
To run it:
Then I extended it with:
125e-2 = 1.25
);^
;You can find it in this Gist, and try it:
parse_number
got a bit out of hand, but I find the rest quite elegant. I like Erlang.Nim.
Did some cleanup using types. No more string->float->string conversions. Adding old solution below!
My solution in js