Given an expression: 2 + ( ( 4 + 6 ) * (9 - 2) - 5 - 1) + 1 Write a program to calculate

Given an expression: 2 + ( ( 4 + 6 ) * (9 - 2) - 5 - 1) + 1

  1. Write a program calculate the given expression

  2. Do not simply use “eval” directly

  3. Need unit test

Bouns:

2 + ( ( 4 + 6 ) * sqrt(5) + 3) / 2 + 1

Here is my code, who can improve this for me? Thank you!

Here is test code.

Without an “x” in that expression I would argue that you don’t need (and shouldn’t have) a unit test.

Now, if you add an “x” to that expression, then your unit test becomes “Given value of X is ___, the result is ___” which makes a certain kind of sense.

But without an “x”, all you’re testing is Ruby’s ability to parse, and C’s ability to do math, which is certainly not within the scope of your use case or domain.

Also I would argue that mathematically the part of the expression that reads (9 - 2) - 5 - 1 is ambiguous in terms of algebra since you need to know if the minus operation is “left order precedence” or “right order precedence”. Personally I don’t have time in my workday to remember that specific nuance of Ruby’s implementation of algebra and if this were a Junior dev submitting a pull request I would make him or her add parenthesis to eliminate that confusion.

Just my 2¢ on your post.

-Jason

Is this homework for school or an interview?