-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalc.py
59 lines (41 loc) · 1.75 KB
/
calc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
class SExpression:
# Pre-Calculated expressions for dynamic programming optimization
pre_calculated = {}
def calc(self, str_input):
while ')' in str_input:
# First check if already been calculated
if str_input in self.pre_calculated:
return self.pre_calculated[str_input]
# Start with the first expression to close (ie. first index of ')')
right_bound = str_input.index(')')
left_bound = str_input[:right_bound].rindex('(')
# Note: Because we're getting the first expression to close,
# it is guaranteed not to have nested functions inside
value = self._evaluate_single(str_input[left_bound + 1:right_bound])
# If evaluated final function
if left_bound == 0:
return value
# Else update str_input, replacing expression with calculated value
else:
str_input = str_input[:left_bound] + str(value) + str_input[right_bound+1:]
return int(str_input)
# Evaluates simple expression with no nested values
def _evaluate_single(self, str_input):
# Check if already been calculated
if str_input in self.pre_calculated:
return self.pre_calculated[str_input]
pieces = str_input.split()
if pieces[0] == 'add':
answer = int(pieces[1]) + int(pieces[2])
elif pieces[0] == 'multiply':
answer = int(pieces[1]) * int(pieces[2])
else:
answer = int(str_input)
# Add to pre calculated dict
self.pre_calculated[str_input] = answer
return answer
def main():
print(SExpression().calc(sys.argv[1]))
if __name__ == '__main__':
main()