-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
52 lines (43 loc) · 1.81 KB
/
logic.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
import re
import math
class Calculation:
def __init__(self, given):
self.given = given.replace(" ", "")
def evaluate(self):
result = self._do_factorials()
result = self._do_sqrt(result)
result = self._do_powers(result)
result = self._do_trig(result)
result = self._do_log(result)
try:
return eval(result)
except:
return "Error"
def _do_factorials(self):
regex = "\d+!"
subs = lambda m: str(math.factorial(int(m.group()[:-1])))
return re.sub(regex, subs, self.given)
def _do_sqrt(self):
regex = "√<\d+>"
subs = lambda m: str(math.sqrt(int(m.group().replace("√<","").replace(">",""))))
return re.sub(regex, subs, self.given)
def _do_powers(self):
regex = "\d+\^<\d+>"
subs = lambda m:str(math.pow(int(m.group().split('^<')[0]),
int(m.group().split('^<')[1].replace(">",""))))
return re.sub(regex, subs, self.given)
def _do_trig(self):
regex = "[a-zA-Z]+<\d+>"
subs = lambda m: self._trig_func(m.group())
return re.sub(regex, subs, self.given)
def _trig_func(self, match):
func, arg = match.split("<")[0], int(match.split("<")[1].replace(">",""))
arg = math.radians(arg)
if func == "cot":
return str(math.tan(1/arg))
return str(getattr(math, func)(arg))
def _do_log(self):
regex = "log\[\d+]<\d+>"
subs = lambda m: str(math.log(int(m.group().split('[')[1].split(']')[0]),
int(m.group().split('<')[1].replace(">",""))))
return re.sub(regex, subs, self.given)