-
Notifications
You must be signed in to change notification settings - Fork 0
/
equation-solver.py
65 lines (51 loc) · 1.92 KB
/
equation-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
def validate_equation(equation_string):
'''Ensures the inputted equation follows the format guidlines'''
# Check for illegal characters
for char in equation_string:
if char.isalpha() or char.isnumeric() or char in ['+', '-', '*', '/', '.', '=']:
pass
else:
return False
# Make sure only 1 variable exists
equation_variable = ''
for char in equation_string:
if char.isalpha():
if equation_variable == '' or equation_variable == char:
equation_variable = char
else:
return False
# Make sure there is exactly 1 equals sign
if not equation_string.count('=') == 1:
return False
return True
def split_equation_terms(equation_string):
'''Converts an equation string into two strings (left and right side).'''
# Make sure only 1 variable exists
equation_variable = ''
for char in equation_string:
if char.isalpha():
if equation_variable == '' or equation_variable == char:
equation_variable = char
else:
raise Exception('Too many variables (more than 1)')
# Set all instances of the variable to 'x' for simplicity
equation_string = equation_string.replace(equation_variable, 'x')
# Replace all instances of - to +- for simplicity
equation_string = equation_string.replace('-', '+-')
# Split the equation in two
halved_equation = equation_string.split('=')
left_side = halved_equation[0]
right_side = halved_equation[1]
# Split each half into terms using the + sign
left_side = left_side.split('+')
try:
left_side.remove('')
except:
pass
right_side = right_side.split('+')
try:
right_side.remove('')
except:
pass
# Return the two sides of the equation
return left_side, right_side