Skip to content

jasonray/RPN-calculator-python

Repository files navigation

Python package Codacy Badge

RPN-calculator-python

The RPN calculator implementation is a good exercise for working through a languages capabilities and exploring OO strategies.

I have done four variations of the RPN calculator as exploratory into languages:

How to run

This implementation utilizes Python.

Prereqs

  • brew
    • xcode-select --install
    • /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • python
    • brew install python

Unit Test

To run the tests, run the following command:

make test

Check formatting

To check style and formatting

make lint

To check formatting

make check-format

To auto format

make format

To add a new operator

  1. Create a new operator implementation class. Add this class to the src/rpn/operators directory. Follow the conventation of (operator name)-operator.py (example: addition-operator.py).

  2. The operator implementation class needs to implement the interface Operator, which will have the class implement the following methods

  • doOperation(RpnStack): this method is responsible for pop-ing the operands from the stack, execute the operation, and push-ing the result back to the stack
  • handlesOperatorCharacter(String): this method is responsible for checking if this class handles a particular operator character. For example, an addition operation class would return true for +.

A sample base implementation for addition is shown below:

class AdditionOperator(Operator):

    def doOperation(self, numbers: RpnStack) -> int:
        rhs = numbers.pop()
        lhs = numbers.pop()
        result = lhs + rhs
        numbers.push(result)
        return result

    def handlesOperatorCharacter(self, operand) -> bool:
        return operand == "+"
  1. Register operator in OperatorRegistry. To do this, add the following statement:
        self._register(AdditionOperator())

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •