Created
March 25, 2014 12:50
-
-
Save starenka/9761210 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# coding=utf-8 | |
import sys, operator, re | |
OPMAP = {'+': operator.add, '-': operator.sub, '/': operator.div, '*': operator.mul} | |
RE_PARSE = re.compile(r'(?P<op>[+\-/\*])\s+(?P<num>\d+)') | |
def apply(line, prev): | |
op, num = re.match(RE_PARSE, line.strip('\n')).groups() | |
return OPMAP[op](prev, float(num)) | |
def do(file_name): | |
with open(file_name) as f: | |
lines = f.readlines() | |
operations = lines[:-1] | |
base = float(lines[-1].split(' ')[1]) | |
return reduce(lambda x,y: apply(y, x), operations, base) | |
if __name__ == '__main__': | |
print do(sys.argv[1]) |
Author
starenka
commented
Mar 25, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment