Adapted from this CodeWars exercise, created by user vgrichina.
RPN, or Reverse Polish Notation,
is a convention for writing arithemetic expressions.
Ordinarily, most arithemetic expressions are written as 1 + 2
: the operation
is in the middle, with arguments (in this case, 1 and 2) on either side.
In Polish Notation, this order is changed so that the operation comes
before all of the arguments, i.e. + 1 2
.
Reverse Polish Notation is almost the same, as Polish Notation, except that
the operation comes after all of the arguments, i.e. 1 2 +
Your challenge will be to create a simplified calculator for evaluating expressions written in RPN.
Please note your calculator should be able to handle expressions with more than one operation.
- None
- Fork and clone this repository.
- Change into the new directory.
- Install dependencies.
- Create and checkout a new branch to work on.
- Fulfill the listed requirements.
Starter code is available in lib/challenge.rb
. A pull
request is not required, but it is necessary if you want a code review.
You may wish to refer to FAQs related to forking, cloning.
Implement a method calc
which, given a string of characters written in RPN,
returns the result of the operations.
For now, the calculator should only implement four operations: +
, -
, *
,
or /
.
Additionally, note that:
- If the expression is empty, it should evaluate to zero.
- The calculator must also work with floating point numbers (i.e. decimals).
For simplicity's sake, assume that all strings are properly formatted, with a single space between every character, and that your calculator will not be given any unusual operations (such as dividing by zero).
You should be running rubocop
before diagnosing any bugs, since it finds
some of the most common sources of errors.
After rubocop
passes your code (ignore any warnings about spec_helper
),
you should run rake test
to run the included tests, which will tell you
whether or not you've met requirements.
- All content is licensed under a CCBYNCSA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].