-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperform_math.py
116 lines (95 loc) · 2.7 KB
/
perform_math.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
import math
def basicOperations(text):
text = text.replace("calculate", "")
text = text.replace("what is", "")
text = text.replace("value of", "")
text = text.replace("what is the value of", "")
text = text.replace("evaluate", "")
text = text.replace("find the value of", "")
text = text.replace("find", "")
if 'root' in text:
temp = text.rfind(' ')
num = int(text[temp+1:])
return round(math.sqrt(num), 2)
text = text.replace('plus', '+')
text = text.replace('minus', '-')
text = text.replace('x', '*')
text = text.replace('multiplied by', '*')
text = text.replace('multiply', '*')
text = text.replace('divided by', '/')
text = text.replace('to the power of', '**')
text = text.replace('to the power', '**')
text = text.replace('power', '**')
result = eval(text)
return round(result, 2)
def bitwiseOperations(text):
if 'right shift' in text:
temp = text.rfind(' ')
num = int(text[temp+1:])
return num >> 1
elif 'left shift' in text:
temp = text.rfind(' ')
num = int(text[temp+1:])
return num << 1
text = text.replace('and', '&')
text = text.replace('or', '|')
text = text.replace('not of', '~')
text = text.replace('not', '~')
text = text.replace('xor', '^')
result = eval(text)
return result
def conversions(text):
temp = text.rfind(' ')
num = int(text[temp+1:])
if 'bin' in text:
return eval('bin(num)')[2:]
elif 'hex' in text:
return eval('hex(num)')[2:]
elif 'oct' in text:
return eval('oct(num)')[2:]
def trigonometry(text):
temp = text.replace('degree', '')
temp = text.rfind(' ')
deg = int(text[temp+1:])
rad = (deg * math.pi) / 180
if 'sin' in text:
return round(math.sin(rad), 2)
elif 'cos' in text:
return round(math.cos(rad), 2)
elif 'tan' in text:
return round(math.tan(rad), 2)
def factorial(n):
if n == 1 or n == 0:
return 1
else:
return n*factorial(n-1)
def logFind(text):
temp = text.rfind(' ')
num = int(text[temp+1:])
return round(math.log(num, 10), 2)
def isHaving(text, lst):
for word in lst:
if word in text:
return True
return False
def perform(text):
text = text.replace('math', '')
if "factorial" in text:
return str(factorial(int(text[text.rfind(' ')+1:])))
elif isHaving(text, ['sin', 'cos', 'tan']):
return str(trigonometry(text))
elif isHaving(text, ['bin', 'hex', 'oct']):
return str(conversions(text))
elif isHaving(text, ['shift', 'and', 'or', 'not']):
return str(bitwiseOperations(text))
elif 'log' in text:
return str(logFind(text))
else:
return str(basicOperations(text))
# print(round(math.log(1,10),2))
if __name__ == "__main__":
while(True):
x = input("enter the text: ")
if(x == 'exit'):
break
print(perform(x))