-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression_solver.py
545 lines (387 loc) · 13.7 KB
/
expression_solver.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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Expressions Solver
------------------
This module validates and solves expressions for games like "Mathler" and "Nerdle".
"""
import re
__author__ = "Lucas Hohmann"
__email__ = "[email protected]"
__user__ = "@lfhohmann"
__status__ = "Production"
__date__ = "2022/03/06"
__version__ = "2.0.0"
__license__ = "MIT"
# TODO: Implement Unit Tests
class ExpressionSolver:
"""
Solver class
------------
A class which contains all of the necessary functions to validate and solve
the passed expression.
>>> solver = ExpressionSolver()
... solver.solve("2-278") -> [276]
"""
def __init__(self, config: dict) -> None:
"""
Init object
-----------
Initializes the object with the passed config.
Parameters
----------
#### config : dict (required)
Dictionary containing the configurations for the rules to validate and
solve the expression.
>>> {
... "allow_consecutive_sum_and_sub_operators": True,
... "allow_leading_zeros": False
... }
Returns
-------
>>> None
"""
# Set config
self.config = config
def __is_string_a_number(self, string: str) -> bool:
"""
Check if a string is a number
--------------
Checks if a passed string is a number or not.
Parameters
----------
#### string : str (required)
String containing the characters to be check
>>> "-1234" -> True
>>> "+4321" -> True
>>> "12.34" -> False
>>> "123ab" -> False
Returns
-------
#### bool:
True or False depending on if the string is a number or not
>>> True
>>> False
"""
# Try to convert to a number, if it suceeds it's a number
try:
int(string)
return True
# If it fails, it's not a number
except ValueError:
return False
def __pre_validate(self, expression: str) -> bool:
"""
Check if an expression is valid before solving it
--------------
Checks if a passed expression as a string is valid or not.
Parameters
----------
#### expression : str (required)
String containing the characters of the expression
>>> "-12+34*2" -> True
>>> "-+2+3*-3" -> True
>>> "-1a*2b+6" -> False
>>> "+/43-432" -> False
Returns
-------
#### bool:
True or False depending on wheter the string is a number or not
>>> True
>>> False
"""
# Only allow numbers and operators
if not re.match(r"^[0-9+-/*]*$", expression):
return False
# First char can't be a "/" or "*" operator
if re.findall(r"^[\/,*]", expression):
return False
# Last char has to be a number
if re.findall(r"\D$", expression):
return False
# There can't be consecutive "/" or "*" operators
if re.findall(r"\D[\/,*]", expression):
return False
# Check for consecutive "+" and "-" operators depending on config
elif not self.config["allow_consecutive_sum_and_sub_operators"]:
if re.findall(r"[+,-]{2}", expression):
return False
# Check for leading zeros depending on config
if not self.config["allow_leading_zeros"]:
if re.findall(r"0\d", expression):
return False
# Expression is valid, return True
return True
def __join_numbers(self, expression: list) -> list:
"""
Join consecutive numbers
--------------
Joins consecutive number characters in the expression.
Parameters
----------
#### expression : list (required)
List containing the characters of the expression
>>> ["1", "2", "3", "+", "3", "2", "1"] -> ["123", "+", "321"]
>>> ["1", "2", "+", "3", "*", "-", "2"] -> ["12", "+", "3", "*", "-", "2"]
Returns
-------
#### list:
A list containing the joined numbers
>>> ["123", "+", "321"]
>>> ["12", "+", "3", "*", "-", "2"]
"""
# Init temporary string and index number
temp = ""
i = 0
# Loop through the expression
while i < len(expression):
# Check if the current element is a number
if expression[i].isnumeric():
# Add the number to the temporary string and pop it
temp += expression[i]
expression.pop(i)
# Insert temp into expression if it's not empty
else:
if temp != "":
expression.insert(i, temp)
# Reset temp and go to the next element
temp = ""
i += 1
# Append the last temp to the expression
expression.append(temp)
# Return the expression
return expression
def __join_operators(self, expression: list) -> list:
"""
Join "+" and "-" operators
--------------
Joins consecutive "+" and "-" operators in the expression.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> ["123", "+", "-", "321"] -> ["123", "-", "321"]
>>> ["12", "+", "3", "*", "-", "-", "2"] -> ["12", "+", "3", "*", "+", "2"]
Returns
-------
#### list:
A list containing the elements of the expression with the joined
operators
>>> ["123", "+", "321"]
>>> ["12", "+", "3", "*", "-", "2"]
"""
# Init index
i = 1
# Loop through the whole expression
while True:
# Check if there are consecutive "+" and "-" operators
if expression[i - 1] in "+-" and expression[i] in "+-":
# If so, join the operators
if expression[i - 1] == expression[i]:
expression[i - 1] = "+"
else:
expression[i - 1] = "-"
# Remove the current element and reset index
expression.pop(i)
i = 1
# Go to next element if current ones are not "+" or "-" operators
else:
i += 1
# Check if we've reached the end of the expression
if i >= len(expression):
break
# Return the expression
return expression
def __join_numbers_to_operators(self, expression: list) -> list:
"""
Join operators to numbers
--------------
Joins "+" and "-" operators to their following numbers.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> ["123", "-", "321"] -> ["123", "-321"]
>>> ["12", "+", "3", "*", "+", "2"] -> ["12", "+3", "*", "+2"]
Returns
-------
#### list:
A list containing the elements of the expression with the joined
operators to numbers
>>> ["123", "-321"]
>>> ["12", "+3", "*", "+2"]
"""
# Init index
i = 1
# Loop through the expression while there are "+" and/or "-" operators
while "+" in expression or "-" in expression:
# Check if previous element is a "+" or "-" operator
if expression[i - 1] in "+-":
# If so, join the operator to the current number element, and pop current element
expression[i - 1] += expression[i]
expression.pop(i)
# Go to next element
i += 1
# Return the expression
return expression
def __convert_numbers_to_ints(self, expression: list) -> list:
"""
Strings to ints
--------------
Converts the numbers from strings into integers.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> ["123", "+0"] -> [123, 0]
>>> ["12", "3", "*", "2"] -> [12, 3, "*", 2]
Returns
-------
#### list:
A list containing the elements of the expression with numbers converted
to integers
>>> [123, 0]
>>> [12, 3, "*", 2]
"""
# Loop through the expression
for i in range(len(expression)):
# Check if the current element is a number
if self.__is_string_a_number(expression[i]):
# Convert the number to an integer
expression[i] = int(expression[i])
# Return the expression
return expression
def __check_for_divisions_by_zero(self, expression: list) -> bool:
"""
Check divisions by zero
--------------
Checks if the expression contains divisions by zero.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> [123, "/", 0] -> True
>>> [12, 3, "*", 2] -> False
Returns
-------
#### bool:
True if the expression contains divisions by zero, False otherwise
>>> True
>>> False
"""
# Loop through the expression comparing to element at a time
for i in range(1, len(expression)):
# Check if the previous element is a division operator and the current element is zero
if expression[i - 1] == "/" and expression[i] == 0:
# Return False if there is a division by zero
return True
# Return True if there are no divisions by zero
return False
def __solve_div_and_mul_operators(self, expression: list) -> list:
"""
Solve "/" and "*"
--------------
Solves division and multiplication operations, it also checks if the
result of the divisions are integers.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> [4, 124, "/", 2, "*", 4] -> [4, 248]
>>> [12, 3, "*", 2] -> [12, 6]
>>> [4, 124, "/", 3, "*", 4] -> []
Returns
-------
#### list:
A list containing the result of the operations with the elements of
the expression
>>> [4, "+", 248]
>>> [12, 6]
>>> []
"""
# Init index
i = 1
# Loop through the expression while there are "/" and/or "*" operators
while "/" in expression or "*" in expression:
# Check if current element is a division or multiplication operator
if expression[i] == "/" or expression[i] == "*":
# Perform the appropriate operation
if expression[i] == "/":
expression[i - 1] /= expression[i + 1]
# Check if the division results in an integer
if not expression[i - 1] % 1 == 0:
return []
else:
expression[i - 1] *= expression[i + 1]
# Remove the current element and the following element
for _ in range(2):
expression.pop(i)
# Go to next element
else:
i += 1
# Return the expression
return expression
def __sum_elements(self, expression: list) -> list:
"""
Sum elements
--------------
Sums all of the elements in the expression.
Parameters
----------
#### expression : list (required)
List containing the elements of the expression
>>> [4, "+", 248] -> [252]
>>> [12, 6] -> [18]
>>> [] -> []
Returns
-------
#### list:
A list containing the result of the sum of the elements as a single
number
>>> [252]
>>> [18]
>>> []
"""
# Return sum only if list is not empty
if expression:
return [int(sum(expression))]
else:
return []
def solve(self, expression: str) -> list:
"""
Solve expression
--------------
Validates and solves an expression and returns the solution as a list,
if invalid returns an empty list.
Parameters
----------
#### expression : str (required)
A string representing the expression to be solved
>>> "4+254/2" -> [131]
>>> "234/0" -> []
Returns
-------
#### list:
A list containing the result of the sum of the elements as a single
number
>>> [131]
>>> []
"""
# Check if the expression is valid
if not self.__pre_validate(expression):
return []
# Split the expression into a list
expression = list(expression)
# Perform the following steps on the expression
expression = self.__join_numbers(expression)
expression = self.__join_operators(expression)
expression = self.__join_numbers_to_operators(expression)
expression = self.__convert_numbers_to_ints(expression)
# Check if the expression has any divisions by zero
if self.__check_for_divisions_by_zero(expression):
return []
# Perform the following steps on the expression
expression = self.__solve_div_and_mul_operators(expression)
expression = self.__sum_elements(expression)
# Return the solution
return expression