Skip to content

Instantly share code, notes, and snippets.

@nicholasren
Created March 28, 2014 06:23
Show Gist options
  • Save nicholasren/9826485 to your computer and use it in GitHub Desktop.
Save nicholasren/9826485 to your computer and use it in GitHub Desktop.
def solveRPN(expression: String):String = {
expression.split(" ").foldLeft(List[String]()) {
(stack:List[String], e:String) =>
e match {
case "*" =>
stack match {
case List(z) => List(z)
case x :: y :: ys => (y.toInt * x.toInt).toString :: ys
}
case "+" =>
stack match {
case List(z) => List(z)
case x :: y :: ys => (y.toInt + x.toInt).toString :: ys
}
case "-" =>
stack match {
case List(z) => List(z)
case x :: y :: ys => (y.toInt - x.toInt).toString :: ys
}
case x => x :: stack
}
}.head
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment