Git Product home page Git Product logo

simplerpn's Introduction

simpleRPN

A very simple Reverse Polish Notation script

RPN stands for Reverse Polish Notation. This is a mathematical notation in which the operators such as ‘+’, ‘-’, ‘/’ and ‘*’, come after the operands, which can be simply numbers. For this reason this is sometimes also called “postfix notation”.

For example, if we want to add two numbers, say 6 and 13, in conventional notation this would be 6 + 13 = 19. In Reverse Polish Notation this is written like 6 13 +.

A more complex example can be ‘1000 990 1 2 + * +’. If we read this line from left to right, the first operator found is ‘+’, right after 1 and 2. So the first step is to add the numbers 1 and 2, which gives 3. At this point we have ‘1000 990 3 * +’. Now the first operator found is ‘*’ right after the 3. Then we make the product between 990 and 3, which is 2970. Now we have ‘1000 2970 +’. Finally, add 1000 with 2970, and the final result is 3970.

To evaluate such expressions in an efficient way, we make use of Stacks. First create an instance of a Stack, this is an empty Stack, and store it in “s”. Define the main operators: ‘+’, ‘-’, ‘/’ and ‘*’, and store them in a list called “ops”. The main idea ihttps://github.com/carlosbionic/simpleRPNs to traverse the input string and parse its operands and operators. These are called “tokens”.

Starting from left to right, we have to “push” each operand (number) onto the stack, until we find an operator. Once we find an operator (by means of an “if” statement), we have to apply that operator to the last two operands before it. This two operands are the last two elements in our Stack (by construction). So we “pop” and save both of them in variables “op1” and “op2”. Finally, simply make the corresponding operation between them and “push” the result back into the Stack.

Repeating this process for the whole input string (“tokens”) will lead to the final result.

Both time complexity and space complexity are O(n), since each operand or operator is pushed and popped from the Stack only once.

simplerpn's People

Contributors

deusdevok avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.