0
\$\begingroup\$

Possible Duplicate:
Reverse Polish notation

Create a function named rpn in python to evaluate a string written in Reverse Polish notation

Numbers must be separated by one or more spaces; operators do not need to be separated by spaces. Must support +, -, * and /.

Return can be any type that int() will accept

Eval is allowed.

Examples:

>>> 3 7 +
10

>>> 1 2+3*4-
5
\$\endgroup\$

marked as duplicate by hallvabo, J B, Peter Taylor, Gareth, gnibbler Dec 21 '11 at 9:46

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \$\begingroup\$ why only python? many languages are good for this \$\endgroup\$ – ratchet freak Dec 20 '11 at 21:24
  • \$\begingroup\$ Should a different character (such as _) be used to represent negative numbers? In your current specification, does 2 3 -7 * mean 2 * 3 * -7 or (2 - 3) * 7? \$\endgroup\$ – Dillon Cower Dec 20 '11 at 21:28
  • \$\begingroup\$ ratchet -- I'm just interested in seeing the interesting solutions people come up with in python, rather than extending the challenge to all languages and having only 1 or 2 python solutions. \$\endgroup\$ – bluepnume Dec 20 '11 at 21:37
  • \$\begingroup\$ D C -- Good point, I've simplified the problem to remove the requirement for negative numbers. \$\endgroup\$ – bluepnume Dec 20 '11 at 21:38
  • 6
    \$\begingroup\$ It is almost the same task as Reverse Polish notation. There are also some answers written in Python. \$\endgroup\$ – Howard Dec 20 '11 at 21:49

Browse other questions tagged or ask your own question.