-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
42 lines (38 loc) · 1.17 KB
/
test.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
import re
import sys
replacements = {
'^': '**',
}
allowed_words = [
'x',
]
def testmathexp(mathexp):
mathexp = mathexp.replace(' ', '')
if mathexp == "":
raise ValueError("please enter Function in terms of x in lowercase ")
for word in re.findall('[a-zA-Z_]+', mathexp):
if word not in allowed_words:
raise ValueError(
'"{}" is forbidden to use in math expression'.format(word)
)
toMatch = "(-)?(\d+$)|((-)?(\d+[+-])?(\d+[\*\/])?[xX](\^\d+)?([+-](\d+)?([\*\/][xX](\^\d+)?)?)*)*$"
matched = re.match(toMatch, mathexp)
if not matched:
raise ValueError("Invalid Expression")
mathexp=mathexp.replace('^','**')
return mathexp
def testnumber(num):
if num == "":
raise ValueError("Enter the limits please")
try:
a=int(num)
return a
except:
raise ValueError("Please enter numbers")
def testlimits(min,max):
if min >= max:
raise ValueError("lower limit should be smaller than upper limit")
def testDivisionByZero(expression, minValue, maxValue):
if expression.find('/x') != -1 and minValue <= 0 and maxValue >= 0:
return False
return True