-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodules.py
More file actions
104 lines (85 loc) · 3.58 KB
/
modules.py
File metadata and controls
104 lines (85 loc) · 3.58 KB
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
def findindexofthecomponent(equation,component) :
for i in range(len(equation)) :
if equation[i] == component :
indexofcomponent = i
else :
continue
return indexofcomponent
def findindedexofthevariable(equation) :
notvariables = "1234567890x/+-="
for i in range(len(equation)) :
counter = 0
for x in range(len(notvariables)) :
if equation[i] != notvariables[x] :
counter += 1
if counter == len(notvariables) :
variableindex = i
return variableindex
def standardform(equation) :
equation = equation.replace(" ","")
if findindedexofthevariable(equation) > findindexofthecomponent(equation,"=") :
part1 = equation[findindexofthecomponent(equation,"=")+1:len(equation)]
part2 = equation[0:findindexofthecomponent(equation,"=")]
equation = part1 + "=" + part2
return equation
def findnumberafterequal(equation) :
equation = standardform(equation)
numberafterequal = equation[findindexofthecomponent(equation,"=")+1:len(equation)]
return int(numberafterequal)
def findtypeoftequation(equation) : # 1 2X/8=10 # 2 8/2X=10
equation = standardform(equation)
if equation[findindedexofthevariable(equation)+1] == "=" :
return 2
else :
return 1
def findoperatorindex(equation) :
found = False
indexofoperators = []
equation = standardform(equation)
equation = equation[0:findindexofthecomponent(equation,"=")]
for i in range(len(equation)) :
if equation[i] == "/" : # 1 -2X/-8=10 # 2 -8/-2X=10
operatorindex = i # 1 -2X-8=10 # 2 -8-2X=10
found = True
elif equation[i] == "x" :
operatorindex = i
found = True
if found == False :
for x in range(len(equation)) :
if equation[x] == "+" or equation[x] == "-" :
indexofoperators.append(x)
if len(indexofoperators) == 2 :
operatorindex = indexofoperators[1]
else:
operatorindex = indexofoperators[0]
return operatorindex
def findnumberbeforex(equation) :
equation = standardform(equation)
if findtypeoftequation(equation) == 1 :
if findindedexofthevariable(equation) == 0 :
numberbeforex = 1
else :
numberbeforex = equation[0:findindedexofthevariable(equation)]
if numberbeforex == "-" :
numberbeforex = -1 # 1 2X/8=10 # 2 8/2X=10
return int(numberbeforex)
else:
if findoperatorindex(equation)+1 == findindedexofthevariable(equation) :
numberbeforex = 1
else :
numberbeforex = equation[findoperatorindex(equation)+1:findindedexofthevariable(equation)]
if numberbeforex == "-" :
numberbeforex = -1
return int(numberbeforex)
def findlastnumber(equation) :
equation = standardform(equation)
if findtypeoftequation(equation) == 1 : # 1 2X/8=10 # 2 8/2X=10
lastnumber = equation[findoperatorindex(equation)+1:findindexofthecomponent(equation,"=")]
return int(lastnumber)
else:
lastnumber = equation[0:findoperatorindex(equation)]
return int(lastnumber)
def findoperator(equation) :
equation = standardform(equation)
operator = equation[findoperatorindex(equation)]
return operator