Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. def solveRPN(input: String): Double = {
  2. input.split(" ").toList.foldLeft(List[Double]()){(out, in) =>
  3. in match {
  4. case "+" => out.dropRight(2) :+ (out.dropRight(1).last + out.last)
  5. case "-" => out.dropRight(2) :+ (out.dropRight(1).last - out.last)
  6. case "*" => out.dropRight(2) :+ (out.dropRight(1).last * out.last)
  7. case "/" => out.dropRight(2) :+ (out.dropRight(1).last / out.last)
  8. case "%" => out.dropRight(2) :+ (out.dropRight(1).last % out.last)
  9. case _ => out :+ in.toDouble
  10. }
  11. }.head
  12. }
  13.  
  14. println(solveRPN("1 3 +"))
  15. println(solveRPN("1 3 + 5 2 - *"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement