-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.py
34 lines (28 loc) · 935 Bytes
/
Calculator.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
expression = ["0", "+", "1", "=" , "1"]
ops = ["-", "=", "+", "/"]
mapdigit = {"0" : 0, "1" : 1 , "2" : 2 ,
"3" : 3, "4" : 4, "5" : 5, "6" : 6,
"7" : 7, "8" : 8, "9" : 9, "10" : 10}
def checkdigit(expression, position):
item = expression[position]
if( item in onetoten):
return item
else:
return ""
def getPlus(expression):
scan = 0
for scan in range(len(expression)):
if(expression[scan] == "+"):
if(checkdigit(expression, scan - 1) != ""):
left = expression[scan - 1]
if(checkdigit(expression, scan + 1) != ""):
right = expression[scan + 1]
tree = ["+", [left, right]]
return tree
else:
return ""
tree = getPlus(expression)
root = tree[0]
left = tree[1][0]
right = tree[1][1]
print(left + root + right + "=" + "1")