r/dailyprogrammer • u/Coder_d00d 1 3 • Mar 12 '15
[2015-03-11] Challenge #205 [Intermediate] RPN
Description:
My father owned a very old HP calculator. It was in reverse polish notation (RPN). He would hand me his calculator and tell me "Go ahead and use it". Of course I did not know RPN so everytime I tried I failed.
So for this challenge we will help out young coder_d00d. We will take a normal math equation and convert it into RPN. Next week we will work on the time machine to be able to send back the RPN of the math equation to past me so I can use the calculator correctly.
Input:
A string that represents a math equation to be solved. We will allow the 4 functions, use of () for ordering and thats it. Note white space between characters could be inconsistent.
- Number is a number
- "+" add
- "-" subtract
- "/" divide
- "x" or "*" for multiply
- "(" with a matching ")" for ordering our operations
Output:
The RPN (reverse polish notation) of the math equation.
Challenge inputs:
Note: "" marks the limit of string and not meant to be parsed.
"0+1"
"20-18"
" 3 x 1 "
" 100 / 25"
" 5000 / ((1+1) / 2) * 1000"
" 10 * 6 x 10 / 100"
" (1 + 7 x 7) / 5 - 3 "
"10000 / ( 9 x 9 + 20 -1)-92"
"4+5 * (333x3 / 9-110 )"
" 0 x (2000 / 4 * 5 / 1 * (1 x 10))"Additional Challenge:
Since you already got RPN - solve the equations.
19
u/IceDane 0 0 Mar 12 '15
Since we're all pedantic programmers, I'd like to point out that these aren't equations, but expressions. There are no unknowns involved -- you're just evaluating the expressions.
5
u/Lyrrad0 Mar 12 '15
It's been a while since I did this sort of program. Here I decided to process the brackets first, followed by multiply/divide, then add/subtract.
Here's my results for the test cases:
The RPN is followed by the result on the following line
Results:
2
u/YuriKahn Mar 15 '15
Omg, I have one of those calculators - a HP 15-C, right? It's the best calculator I've ever owned.
3
u/Godspiral 3 3 Mar 12 '15
In J,
everything (functions) in J is an infix operator. So can use an adverb to modify any operator (verb). Using native division in J is the % operator.
5000 1 1 + 2 % 1000 * %
1 7 7 * + 5 3 - %
to auto convert raw expressions:
3
u/binaryblade Mar 13 '15 edited Mar 13 '15
*go, first take at this. Comments are welcome.
package main
EDIT: whoops, I think I did polish notation instead of reverse polish, oh well same diff. EDIT2: found and error and updated also here is the output
2
u/hutsboR 3 0 Mar 12 '15 edited Mar 12 '15
Elixir: Somewhat elegant, parses all of the challenge inputs properly. Uses Shunting-yard algorithm.
Usage:
2
u/Godd2 Mar 12 '15
Here's my solution in Ruby. It's somewhat of a mess, but it works, and I'm fairly proud of it.
And the output:
I went ahead and replaced any x's with *'s.
2
u/somejeff_ Mar 13 '15 edited Mar 13 '15
JavaScript:
2
u/franza73 Mar 13 '15
2
u/h2g2_researcher Mar 16 '15
I know I'm a little late. C++ solution. Infix is parsed recursively: each half is added to the stack, and then each half is parsed. It leads to some slightly odd (but otherwise correct) results. I also did the additional challenge.
1
u/dtconcus Mar 12 '15
Ruby! First off, I'd like to apologize for the method names. I actually had to implement a grammar tree for equations for school a while back (using Java), so I just used that as basis and added some functionality to actually output the infix notation (my school project just had them solve the equation).
1
u/[deleted] Mar 12 '15 edited Mar 12 '15
Java: Some of the spacing is off, but I think it works otherwise. (This is a lot of fun, btw.)
1
u/adrian17 1 4 Mar 12 '15 edited Mar 12 '15
Python 3, shunting yard,
without the bonusAdded bonus.1
u/heap42 Mar 12 '15 edited Mar 12 '15
Some Python3. I still have to figure out a smooth way how to deal with numbers that have more digits probably gonna do something with lists... but here is the code that works already... just not nicely.
1
u/krismaz 0 1 Mar 12 '15
Python3, using the
tokenizerIncludes bonus1
u/TotesMessenger Mar 12 '15
This thread has been linked to from another place on reddit.
If you follow any of the above links, respect the rules of reddit and don't vote. (Info / ^Contact)
1
u/T-Fowl Mar 17 '15
Scala, which I am still fairly new to.
1
u/[deleted] Mar 21 '15
Here is the solution in Rust 1.0.0-nightly (ea8b82e90 2015-03-17) (built 2015-03-18). It took me some time while I get familiar with enums, tokenizing and using the match statements properly. I think this can easily be extended to support other operators as well because of the structure I chose for it.
1
u/fbWright Mar 21 '15
Python 3
Basically a (somewhat verbose) implementation of the shunting-yard algorithm. Raises a
SyntaxErroron mismatched parentheses or when encountering unknown characters when tokenizing.1
u/pabs Mar 22 '15
Ruby, subset of Shunting-yard algorithm:
Output:
0
u/wizao 1 0 Mar 12 '15 edited Mar 14 '15
Haskell:
I implemented a LL(1) parser using attoparsec. Here's my updated grammar and code:
source:
Thanks to /u/marchelzo who pointed out my original grammar wasn't left associative which lead me to my
chainl1solution.