Create mathrandom-progressive.py
Browse filesIt allows creating progressive sets easily.
- mathrandom-progressive.py +46 -0
mathrandom-progressive.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import json
|
| 3 |
+
import operator
|
| 4 |
+
|
| 5 |
+
num_samples = [20000, 50000, 230000, 500000]
|
| 6 |
+
min_values = [-9.99, -19.99, -99.99, -99.99]
|
| 7 |
+
max_values = [9.99, 19.99, 99.99, 99.99]
|
| 8 |
+
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
|
| 9 |
+
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
|
| 10 |
+
operations = {
|
| 11 |
+
'+': operator.add,
|
| 12 |
+
'-': operator.sub,
|
| 13 |
+
'*': operator.mul,
|
| 14 |
+
'/': operator.truediv
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def safe_division(numerator, denominator):
|
| 18 |
+
return numerator if denominator == 0 else numerator / denominator
|
| 19 |
+
|
| 20 |
+
for idx, _ in enumerate(num_samples):
|
| 21 |
+
data = []
|
| 22 |
+
for _ in range(num_samples[idx]):
|
| 23 |
+
num1, num2, num3 = None, None, None
|
| 24 |
+
while num1 in [None, 0., -0.] or num2 in [None, 0., -0.] or num3 in [None, 0., -0.]:
|
| 25 |
+
num1 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
|
| 26 |
+
num2 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
|
| 27 |
+
num3 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
|
| 28 |
+
|
| 29 |
+
operation1 = random.choice(list(operations.keys()))
|
| 30 |
+
operation2 = random.choice(list(operations.keys()))
|
| 31 |
+
|
| 32 |
+
if random.choice([True, False]):
|
| 33 |
+
expression = f"({num1} {operation1} {num2}) {operation2} {num3}"
|
| 34 |
+
intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
|
| 35 |
+
result = safe_division(intermediate_result, num3) if operation2 == '/' and intermediate_result != 0 else operations[operation2](intermediate_result, num3)
|
| 36 |
+
else:
|
| 37 |
+
expression = f"{num1} {operation1} {num2} {operation2} {num3}"
|
| 38 |
+
intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
|
| 39 |
+
result = safe_division(intermediate_result, num3) if operation2 == '/' else operations[operation2](intermediate_result, num3)
|
| 40 |
+
|
| 41 |
+
output = result_floats[idx] % result
|
| 42 |
+
data.append({'instruction': expression, 'output': output})
|
| 43 |
+
|
| 44 |
+
out_file = f'arithmetic-float-complex_{idx}.json'
|
| 45 |
+
with open(out_file, 'w') as f:
|
| 46 |
+
json.dump(data, f)
|