-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpr.py
More file actions
28 lines (26 loc) · 922 Bytes
/
expr.py
File metadata and controls
28 lines (26 loc) · 922 Bytes
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
import random
def expr(depth):
if depth==1:
return random.choice(['', '-']) + str(int(random.choice(range(1,999))))
exp1 = expr(depth-1)
exp2 = expr(depth-1)
if eval(exp1) % eval(exp2) == 0:
return '(' + exp1 + '/' + exp2 + ')'
return '(' + exp1 + random.choice(['+','-','*']) + exp2 + ')'
expr_num = int(input('Enter amount of expressions you want: '))
print("Generating test files")
with open("in.txt" ,'w') as in_file:
with open("exp.txt", 'w') as exp_file:
n = 0
while(n < expr_num):
exp = expr(random.choice(range(1,10)))
try:
res = eval(exp)
if abs(res) > 2147483647:
continue
except:
continue
if(res != 0):
n += 1
in_file.write(exp +"\n")
exp_file.write("Result is: " + str(res) + "\n")