-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpr_generator.py
More file actions
42 lines (30 loc) · 831 Bytes
/
expr_generator.py
File metadata and controls
42 lines (30 loc) · 831 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import random as rand
import array as arr
max_appr_length = 500
rules = [
["i"],
["(E)"],
["E+E"],
["E-E"],
["E*E"],
["E/E"],
#["E//E"]
]
expression = ""
expression += rand.choice(rules)[0]
only_terminals = False
while not only_terminals:
if len(expression) > max_appr_length:
expression = expression.replace("E", rules[0][0])
if expression.find("E") >= 0:
expression = expression.replace("E", rand.choice(rules)[0], 1)
else:
only_terminals = True
only_imm_values = False
while not only_imm_values:
if expression.find("i") >= 0:
expression = expression.replace("i", str(rand.randint(1, 100)), 1)
else:
only_imm_values = True
print(expression)
print(float.hex(float(eval(expression))))