Skip to content

Instantly share code, notes, and snippets.

@starenka
Created March 25, 2014 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starenka/9761210 to your computer and use it in GitHub Desktop.
Save starenka/9761210 to your computer and use it in GitHub Desktop.
#!/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])
@starenka
Copy link
Author

+ 5
+ 20
- 15
/ 3
* 2
apply 5

starenka /tmp % python fap.py faps
10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment