-
Notifications
You must be signed in to change notification settings - Fork 0
/
equationSolver.py
156 lines (121 loc) · 2.85 KB
/
equationSolver.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import random
def evaluate2pass(exp):
operator = []
operand = []
ops = ['+','-','/','*']
i = 0
n = len(exp)
op = 0
prev = 0
while i < n:
ch = exp[i]
if ch == ' ' or ch =='\t':
i += 1
continue
if ch in ops:
op = ch
operator.append(ch)
i += 1
else:
num, i = getNumber(exp,i)
operand.append(num)
if op in ['*','/']:
oper = operator.pop()
y = operand.pop()
x = operand.pop()
operand.append(calculate(x,y,oper))
if len(operand) == 1:
return operand.pop()
res = operand[0]
j = 1
for oper in operator:
res = calculate(res,operand[j],oper)
j += 1
return res
def calculate(x,y,oper):
if oper == "*":
return x * y
elif oper == '/':
return x / y
elif oper == '+':
return x + y
elif oper == '-':
return x - y
def getNumber(exp,start):
num = ""
while start<len(exp):
ch = exp[start]
if exp[start] ==' ' or exp[start] == '/t':
start += 1
continue
if (ch >= '0' and ch <='9') or ch==".":
num += ch
else:
break
start += 1
return float(num),start
def evaluatePostfix(exp):
postfix_eq= []
operators = ['+','-','/','*']
ops = []
i = 0
n = len(exp)
while i < n:
if exp[i] ==' ' or exp[i] == '/t':
i += 1
continue
if (exp[i] in operators):
while ops and preced(ops[-1],exp[i]):
postfix_eq.append(ops.pop())
ops.append(exp[i])
op = exp[i]
i += 1
else:
num,i = getNumber(exp,i)
postfix_eq.append(num)
while ops:
postfix_eq.append(ops.pop())
return postfixCalculate(postfix_eq)
def preced(op1, op2):
if op1 == '*' or op1 == '/':
return True
if op1 == '+' or op1 == '-':
if op2 == '+' or op2 == '-':
return True
return False
def postfixCalculate(exp):
stack = []
for e in exp:
if e in ['+','-','/','*']:
y = stack.pop()
x = stack.pop()
# print(x," ",y," ",e)
res = calculate(float(x),float(y),e)
stack.append(res)
else:
stack.append(e)
return stack.pop()
def generate_expression(nOperators):
ops = ['+', '/', '*', '-']
d = random.random() * 100
expression = []
expression.append(d)
for i in xrange(0, nOperators):
c = ops[random.randint(0 , 3)]
d = random.random() * 100
if c == '/' and d == 0:
d = d + 1
expression.append(c)
expression.append(d)
return "".join(map(str, expression))
def test(expression):
result_lib = eval(expression)
result = evaluate2pass(expression)
result_postfix = evaluatePostfix(expression)
print("Correct Answer ", result_lib," 2 Pass:",result,"Post Fix",result_postfix)
assert result_lib == result
assert result_lib == result_postfix
test("5*2/2")
for i in xrange(1, 3000, 1000):
expression = generate_expression(i)
test(expression)