Created
September 20, 2011 01:20
-
-
Save mattbasta/1228076 to your computer and use it in GitHub Desktop.
Prefix notation math interpreter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def gen(data): | |
def g(): | |
for token in data.split(" "): | |
yield token | |
return g().next | |
defs = {"+": lambda x, y: x + y, | |
"-": lambda x, y: x - y, | |
"*": lambda x, y: x * y, | |
"/": lambda x, y: x / y} | |
def parser(data): | |
op = data() | |
if op not in defs: | |
return op | |
val1 = int(parser(data)) | |
val2 = int(parser(data)) | |
return defs[op](val1, val2) | |
import sys | |
data = sys.stdin.readline().strip() | |
print parser(gen(data)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment