RPN calculator requirements are confusing to me

In viewing the Specs for the RPN exercise, if I have the following stack:
[2, 3, 4, +]
and evaluate it I should get 7 back.

However, according to wikipedia and basic logic, this should actually result in an error because after applying the plus operator you will have a stack with 2 terms left [2, 7] and no operators. The value method of the RPN should evaluate the entire stack as it exists. I don’t see why is expects to only evaluate a single term.

This doesn’t seem to mesh with the test on line 98 “resolves operator precedence unambiguously”. In that test, when running value it expects the entire input to be evaluated.

Can someone help me understand which behavior is correct?

Hey Adam, it is fine to be confused since you are right, the way the code is being evaluated is different from the way the actual RPN calculations occur. However, that is because the RPN calculator you are working on isn’t complete yet.

For now you are just setting up individual operations. So when you call “+” at the end, it should add up the whole array like you mentioned. But once you reach the extra credit, which involves tokenizing a string and then evaluating it, your RPN calculator will behave more like the actual thing. Hang in there.

1 Like