Skip to content

Instantly share code, notes, and snippets.

@schneems
Created February 9, 2012 17:57
Show Gist options
  • Select an option

  • Save schneems/1781602 to your computer and use it in GitHub Desktop.

Select an option

Save schneems/1781602 to your computer and use it in GitHub Desktop.
RPN

Calculating Reverse Polish Notation

Write an implementation that returns the correct results given an array in parsed RPN

[9]                         #=> 9
[9,1,'+']                   #=> 10
[9,1,'+',5,'+']             #=> 15
[3, 5, '+', 7, 6, '+', '/'] #=> 0.6153846153846154

Generating Reverse Polish Notation Array from String

Optional

"10 11 +"      #=> [10, 11, '+']
"10 11 + 5 +"  #=> [10, 11, '+', 5, '+' ]

Convert Standard Notation to Reverse Polish Notation (string or Array)

Optional

"10 + 11"        #=> "10 11 +"
"(10 + 11) + 5"  #=> "10 11 + 5 +"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment