r/dailyprogrammer • u/Blackshell 2 0 • Nov 10 '16
[2016-11-09] Challenge #291 [Intermediate] Reverse Polish Notation Calculator
A little while back we had a programming challenge to convert an infix expression (also known as "normal" math) to a postfix expression (also known as Reverse Polish Notation). Today we'll do something a little different: We will write a calculator that takes RPN input, and outputs the result.
Formal input
The input will be a whitespace-delimited RPN expression. The supported operators will be:
+- addition-- subtraction*,x- multiplication/- division (floating point, e.g.3/2=1.5, not3/2=1)//- integer division (e.g.3/2=1)%- modulus, or "remainder" division (e.g.14%3=2and21%7=0)^- power!- factorial (unary operator)
Sample input:
0.5 1 2 ! * 2 1 ^ + 10 + *Formal output
The output is a single number: the result of the calculation. The output should also indicate if the input is not a valid RPN expression.
Sample output:
7Explanation: the sample input translates to 0.5 * ((1 * 2!) + (2 ^ 1) + 10), which comes out to 7.
Challenge 1
Input: 1 2 3 4 ! + - / 100 *
Output: -4
Challenge 2
Input: 100 807 3 331 * + 2 2 1 + 2 + * 5 ^ * 23 10 558 * 10 * + + *
Finally...
Hope you enjoyed today's challenge! Have a fun problem or challenge of your own? Drop by /r/dailyprogrammer_ideas and share it with everyone!
8
u/chunes 1 2 Nov 10 '16 edited Nov 10 '16
This is completely trivial in Factor due to it being a concatenative language. Just have to set up some aliases:
Now we can paste the input directly to the REPL:
A proper command line program that parses an input string looks something like this:
1
u/Blackshell 2 0 Nov 10 '16
Python 3:
4
u/LOOKITSADAM 0 0 Nov 11 '16
Java: Lambda Abuse!
1
u/rjckE Nov 10 '16
According to this, Challenge 1 output should be -4 instead of 4.
3
u/Poseprix Nov 10 '16
Go. Trying to learn some more go. Reads input from stdin, parsing each line until it receives no more input. I'm still fairly new to Go, so any suggestions for improvements would be appreciated.
Output
3
u/eMkaQQ Nov 10 '16
PL/SQL
Output:
3
u/ASpueW Nov 10 '16 edited Nov 13 '16
Rust
Challenge 1 output:
Challenge 2 output:
2
u/xavion Nov 10 '16
Javascript
Challenge 2
2
u/jeaton Nov 10 '16
Python 3
2
u/oddolatry Nov 11 '16
Clojure
Quick. Dirty. Revised.
2
u/Tetsumi- 1 0 Nov 11 '16
Racket
This program transform the RPN expression into a S-Expression which is then evaluated as Racket code (Thanks to Homoiconicity).
become
And
become
2
u/karrash76 Nov 15 '16 edited Nov 15 '16
Java begginer, today learning regex and ArrayList :)
1
u/gercunderscore4 Nov 10 '16
Python 2.7
1
u/rjckE Nov 10 '16 edited Nov 10 '16
Java
Challenge #2 output:
1
u/gigantea Nov 10 '16
C++
1
u/tpbvirus Nov 10 '16 edited Nov 10 '16
Java
1
u/marchelzo Nov 10 '16
Ty
1
u/uncleozzy Nov 10 '16
Python 3.5:
1
u/[deleted] Nov 10 '16
Common Lisp:
Solution
There are probably better ways to solve this, but this was my first lisp solution ever done (trying to do it more functional to boot).
1
u/moeghoeg Nov 10 '16 edited Nov 12 '16
Racket:
Edit: added simple error handling.
Edit2: Inspired by JaumeGreen's Common Lisp solution, I added "stack" as an optional argument to "evaluate". Might be useful for multiple inputs on the same stack (not supported in I/O in this case though)
1
u/ichabod801 Nov 10 '16
Another Python solution, using the operator package and my own (efficient) factorial function.
1
u/[deleted] Nov 10 '16
Go. Go doesn't have a stack implementation in the stdlib. Rather than write my own I grabbed another package to handle that part.
1
u/dangerbird2 Nov 10 '16
Common lisp, implemented as a language macro (using alexandria and serapeum utility libraries)
1
u/Minolwa Nov 10 '16 edited Nov 10 '16
Scala
I'm accumulating all of my solutions over on my Github! Go check it out!
This was generated by a script.
1
u/Scroph 0 0 Nov 10 '16 edited Nov 10 '16
C++11 solution. I tried to make it as DRY as possible :
1
u/[deleted] Nov 10 '16
C++, trying to learn STL.
Sample
Challenge #1
Challenge #2
1
u/glider97 Nov 10 '16 edited Nov 21 '16
C
Semi-solution, because it prints the result as a double. I'll fix that later...maybe. Here is the code (~200 lines...yeah). I'll accept any criticism, whether it be about style, memory leaks, failed test cases, etc. I have a feeling there is some memory I'm not freeing.
Edit: Huzzah! Code now prints only up to the right-most significant digit.
1
u/den510 Nov 10 '16
I have a 9 line method in Python 3, but I'm expanding it a bit for readability purposes. IMHO, golfing is fun, but not always practical for sharing.
+/u/CompileBot Python3
1
u/free2use Nov 10 '16
Clojure, have some problems with rounding and extra conversion because of lists vs vector stuff. Would appreciate some feedback)
Result for last input
1
u/olzd Nov 10 '16 edited Nov 10 '16
Common Lisp, as a DSL with macros:
Results:
1
u/franza73 Nov 10 '16 edited Nov 10 '16
Python 2.7
1
u/itsme86 Nov 10 '16
C#
1
u/[deleted] Nov 11 '16 edited Nov 11 '16
Mathematica
1
u/davanger Nov 11 '16
C#
Sample input:
Challenge 1:
Challenge 2:
1
u/codeman869 Nov 11 '16
Ruby A little late to the game.
1
u/Maleval Nov 11 '16
Python 3.5
Decided to do this in the form of a class because I like making helper functions and a class would allow me to organise it all relatively neatly. This is likely completely unnecessary.
Any feedback would be appreciated.
Challenge 1 output:
Challenge 2 output:
1
u/glenbolake 2 0 Nov 11 '16
I did a basic solution to this in Python back when the first challenge happened, just for giggles, but I used
eval, and I'm not a fan of that when I can avoid it. We didn't have unary operations with that one either. So here's my solution without eval.+/u/CompileBot Python3
1
u/jezzi_dailyprogram Nov 11 '16
C-ish compiled as C++.
How it works:
Code:
Output: Note: CMD treats ^ differently so "^" is used.
1
u/boiling_tunic Nov 11 '16 edited Nov 11 '16
Ruby
Does the whole thing back to front.
Please feel free to question/criticise/comment.
Edit: formatting.
1
u/[deleted] Nov 11 '16
Java
1
u/[deleted] Nov 12 '16
Haskell:
1
u/[deleted] Nov 12 '16
Crystal:
https://play.crystal-lang.org/#/r/1dtj
1
u/pie__flavor Nov 13 '16 edited Nov 14 '16
+/u/CompileBot Scala
Input:
1
u/wlivengo Nov 13 '16
Javascript:
Challenge:
1
u/[deleted] Nov 13 '16
Here's a Python 3 solution I wrote a long time ago... I wanted it to be one line long, which means the code is really garbled. Luckily, even though I didn't comment the code I had designed it so I could add more operators anytime. I only had to add three extra operators to get it to work. It won't wrap correctly in reddit so I have to put it into a gist.
https://gist.github.com/u8y7541/c8c5817476742325bf085b2036b6e95b
It took a lot of magic to compress it down this much...
1
u/[deleted] Nov 14 '16
C#
This is literally the first thing that I have ever coded in C#, any feedback is welcome!
Output:
1
u/Stan-It Nov 14 '16
Python
1
u/gju_ Nov 14 '16
Yet another Python implementation
1
u/theelous3 Nov 15 '16 edited Nov 16 '16
Python 3.5, recursive.
I wanted to do it without lambdas or assigning string versions of operators to functions, and recursively. Eval is fine for this imo. If you wipe your file sys while trying to crunch some numbers, you deserve it.
Could code golf it a bit, but it's complex enough as is. Side benifit, inserting a print shows the equasion being simplified quite nicely, such as
Also have not fully tested this. Quickly messing with it before I run out, all seems fine.
Inputs like '10 2 2 * 3 + *' return 70 as expected.
1
u/luther9 Nov 16 '16
Go:
1
u/[deleted] Nov 20 '16 edited Nov 20 '16
C Fun sunday afternoon project to get more familiar with C! Would like to make my switch more compact (the one I use to id the operator), but not sure how. Any on comments how to make it more compact with the same readability & functionality would be nice!
1
u/Ge_O Nov 23 '16
C#
1
u/Rasaford Nov 26 '16
Java
took me some time but here's my version of it. It was very fun so thanks a lot for posting that one on here.
1
u/oureux Jan 13 '17
Ruby I'm new to learning Ruby, any comments are appreciated.
Output
1
u/Executable_ May 06 '17 edited May 06 '17
Loved that challenge :)
Python3
+/u/CompileBot python
0
u/[deleted] Nov 10 '16
[deleted] — view deleted comment
0
u/[deleted] Nov 10 '16
[deleted] — view deleted comment
1
u/[deleted] Nov 10 '16
[removed] — view removed comment