-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate.go
executable file
·268 lines (255 loc) · 6.33 KB
/
evaluate.go
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package main
import (
"errors"
"strconv"
"strings"
)
const NUMBERS = "0123456789"
const OPERATORS = "+-*/"
var OP_PRECDENCE = map[rune]int{
'*': 1,
'/': 1,
'+': 0,
'-': 0,
}
func validChars(equation string, include_equal bool) bool {
var letters string
if include_equal {
letters = NUMBERS + OPERATORS + "="
} else {
letters = NUMBERS + OPERATORS
}
for _, c := range equation {
if !strings.ContainsRune(letters, c) {
return false
}
}
return true
}
func precedence(x, y rune) int {
if !strings.ContainsRune(OPERATORS, x) || !strings.ContainsRune(OPERATORS, y) {
return 0
}
return OP_PRECDENCE[x] - OP_PRECDENCE[y]
}
func shuntingYard(expression string) string {
// convert infix to postfix
ops_stack := make([]rune, len(expression))
ops_last := -1
var output string = ""
prior := 0
for i, c := range expression {
if strings.ContainsRune(OPERATORS, c) {
if c == '-' && (i == 0 || strings.ContainsRune(OPERATORS, rune(expression[i-1]))) {
prior++
} else {
if ops_last != -1 && precedence(c, ops_stack[ops_last]) <= 0 {
for ops_last >= 0 && precedence(c, ops_stack[ops_last]) <= 0 {
output += string(ops_stack[ops_last])
ops_last--
}
}
ops_last++
ops_stack[ops_last] = c
}
} else {
if i != len(expression)-1 && strings.ContainsRune(NUMBERS, rune(expression[i+1])) {
prior++
} else {
output += "(" + expression[i-prior:i+1] + ")"
prior = 0
}
}
}
for i := ops_last; i >= 0; i-- {
output += string(ops_stack[i])
}
return output
}
func postfixCalc(expression string) (float64, error) {
if len(expression) == 0 {
return 0, errors.New("empty expression")
}
// evaluate postfix expression
stack := make([]float64, len(expression))
stack_loc := -1
last_open := 0
last_closed := -1
for i, c := range expression {
if c == '-' && last_open > last_closed {
} else if strings.ContainsRune(OPERATORS, c) {
stack_loc -= 1
if stack_loc < 0 {
return 0, errors.New("missing operands")
}
switch c {
case '+':
stack[stack_loc] = stack[stack_loc] + stack[stack_loc+1]
case '-':
stack[stack_loc] = stack[stack_loc] - stack[stack_loc+1]
case '*':
stack[stack_loc] = stack[stack_loc] * stack[stack_loc+1]
case '/':
if stack[stack_loc+1] == 0 {
return 0, errors.New("divide by zero")
}
stack[stack_loc] = stack[stack_loc] / stack[stack_loc+1]
}
} else if c == ')' {
stack_loc++
new, err := strconv.Atoi(expression[last_open+1 : i])
if err != nil {
return 0, errors.New("double negative")
}
stack[stack_loc] = float64(new)
last_closed = i
} else if c == '(' {
last_open = i
}
}
return stack[0], nil
}
func IsEqual(equation string) (bool, error) {
// returns the truth value of the equation
if !validChars(equation, true) {
return false, errors.New("invalid character")
}
sides := strings.Split(equation, "=")
if len(sides) < 2 {
return false, errors.New("no equality")
} else if len(sides) > 2 {
return false, errors.New("multiple equalities")
}
left := shuntingYard(sides[0])
right := shuntingYard(sides[1])
left_val, err := postfixCalc(left)
if err != nil {
return false, err
}
right_val, err := postfixCalc(right)
if err != nil {
return false, err
}
return left_val == right_val, nil
}
func LeadingZeros(puzzle string) bool {
for i, c := range puzzle {
if c == '0' && i != len(puzzle)-1 && strings.ContainsRune(NUMBERS, rune(puzzle[i+1])) {
return true
}
}
return false
}
func NegativeZeros(puzzle string) bool {
searching_for_num := false
for i, c := range puzzle {
if c == '0' && i > 0 && puzzle[i-1] == '-' && (i < 2 || strings.ContainsRune(OPERATORS+"=", rune(puzzle[i-2]))) {
searching_for_num = true
} else if searching_for_num {
if strings.ContainsRune(NUMBERS, c) && c != '0' {
searching_for_num = false
} else if strings.ContainsRune(OPERATORS+"=", c) {
return true
}
}
}
return false
}
func ContainsOperator(puzzle string) []bool {
expressions := strings.Split(puzzle, "=")
results := make([]bool, len(expressions))
for i, e := range expressions {
results[i] = false
for _, c := range OPERATORS {
if strings.ContainsRune(e, c) {
results[i] = true
break
}
}
}
return results
}
func MultiplicationByZero(puzzle string) (bool, error) {
_, err := IsEqual(puzzle)
if err != nil {
return false, err
}
expressions := strings.Split(puzzle, "=")
for _, e := range expressions {
e = shuntingYard(e)
// evaluate postfix expression
stack := make([]float64, len(e))
stack_loc := -1
last_open := 0
last_closed := -1
for i, c := range e {
if c == '-' && last_open > last_closed {
} else if strings.ContainsRune(OPERATORS, c) {
stack_loc -= 1
switch c {
case '+':
stack[stack_loc] = stack[stack_loc] + stack[stack_loc+1]
case '-':
stack[stack_loc] = stack[stack_loc] - stack[stack_loc+1]
case '*':
if stack[stack_loc] == 0 || stack[stack_loc+1] == 0 {
return true, nil
}
stack[stack_loc] = stack[stack_loc] * stack[stack_loc+1]
case '/':
stack[stack_loc] = stack[stack_loc] / stack[stack_loc+1]
}
} else if c == ')' {
stack_loc++
new, _ := strconv.Atoi(e[last_open+1 : i])
stack[stack_loc] = float64(new)
last_closed = i
} else if c == '(' {
last_open = i
}
}
}
return false, nil
}
func DivisionOfZero(puzzle string) (bool, error) {
_, err := IsEqual(puzzle)
if err != nil {
return false, err
}
expressions := strings.Split(puzzle, "=")
for _, e := range expressions {
e = shuntingYard(e)
// evaluate postfix expression
stack := make([]float64, len(e))
stack_loc := -1
last_open := 0
last_closed := -1
for i, c := range e {
if c == '-' && last_open > last_closed {
} else if strings.ContainsRune(OPERATORS, c) {
stack_loc -= 1
switch c {
case '+':
stack[stack_loc] = stack[stack_loc] + stack[stack_loc+1]
case '-':
stack[stack_loc] = stack[stack_loc] - stack[stack_loc+1]
case '*':
stack[stack_loc] = stack[stack_loc] * stack[stack_loc+1]
case '/':
if stack[stack_loc] == 0 {
return true, nil
}
stack[stack_loc] = stack[stack_loc] / stack[stack_loc+1]
}
} else if c == ')' {
stack_loc++
new, _ := strconv.Atoi(e[last_open+1 : i])
stack[stack_loc] = float64(new)
last_closed = i
} else if c == '(' {
last_open = i
}
}
}
return false, nil
}