-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgebricExpressions.py
118 lines (100 loc) · 3.19 KB
/
AlgebricExpressions.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
import re
def add(expression):
result = expression
# let's just take both operands first
expRegex = re.compile(r'([0-9]?[a-zA-Z])\+([0-9]?[a-zA-Z])') # selects both co-efficients and variables.
operands = expRegex.search(expression)
operand1 = operands.group(1)
operand2 = operands.group(2)
# let's check for the co-efficients if present
op1Coef = ""
op2Coef = ""
for x in operand1:
if x.isnumeric():
op1Coef += x
else:
pass
for y in operand2:
if y.isnumeric():
op2Coef += y
else:
pass
# print (op1Coef, "+", op2Coef)
if op1Coef == "":
op1Coef = "1"
if op2Coef == "":
op2Coef = "1"
# selecting only the variables
var1 = ''.join(filter(str.isalpha, operand1))
var2 = ''.join(filter(str.isalpha, operand2))
if(var1 == var2):
result = str(int(op1Coef)+int(op2Coef))+var1
else:
pass
# print(var1, var2)
print(result)
def mul(expression):
result = expression
# let's just take both operands first
expRegex = re.compile(r'([0-9]?[a-zA-Z]\^?[0-9]?)\*([0-9]?[a-zA-Z]\^?[0-9]?)') # selects both co-efficients and variables.
operands = expRegex.search(expression)
operand1 = operands.group(1)
operand2 = operands.group(2)
# print(operand1, operand2)
# let's check for the co-efficients if present
op1Coef = ""
op2Coef = ""
for x in operand1:
if x == "^":
break
if x.isnumeric():
op1Coef += x
else:
pass
for y in operand2:
if y == "^":
break
if y.isnumeric():
op2Coef += y
else:
pass
# print (op1Coef, "*", op2Coef)
if op1Coef == "":
op1Coef = "1"
if op2Coef == "":
op2Coef = "1"
resultCoef = str(int(op1Coef)*int(op2Coef))
if "^" in operand1:
expRegex = re.compile(r'(\^)([0-9])') # selects power of variable 1.
power = expRegex.search(operand1)
op1Power = power.group(2)
else:
op1Power = 1
if "^" in operand2:
expRegex = re.compile(r'(\^)([0-9])') # selects power of variable 2.
power = expRegex.search(operand2)
op2Power = power.group(2)
else:
op2Power = 1
# print(op1Power, op2Power)
var1 = ''.join(filter(str.isalpha, operand1))
var2 = ''.join(filter(str.isalpha, operand2))
if(var1 == var2):
result = resultCoef + var1 + superScript(str(int(op1Power)+int(op2Power)))
elif(var1 != var2):
result = resultCoef + var1 + superScript(str(op1Power)) + var2 + superScript(str(op2Power))
else:
pass
print(result)
# function to convert to superscript
def superScript(e):
normal = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-=()"
super_s = "ᴬᴮᶜᴰᴱᶠᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾQᴿˢᵀᵁⱽᵂˣʸᶻᵃᵇᶜᵈᵉᶠᵍʰᶦʲᵏˡᵐⁿᵒᵖ۹ʳˢᵗᵘᵛʷˣʸᶻ⁰¹²³⁴⁵⁶⁷⁸⁹⁺⁻⁼⁽⁾"
res = e.maketrans(''.join(normal), ''.join(super_s))
return e.translate(res)
# add("a+a")
# add("2a+3b")
# add("2a+3a")
mul("a^3*a^4")
mul("2a^2*3b^3")
mul("2a*3a")