Sina Media Lab
commited on
Commit
·
26f260d
1
Parent(s):
c7df340
Updates
Browse files- modules/subtraction_bases.py +41 -28
modules/subtraction_bases.py
CHANGED
@@ -1,44 +1,57 @@
|
|
1 |
-
|
2 |
|
3 |
title = "Subtraction in Bases"
|
4 |
-
description = "This module
|
5 |
|
6 |
-
def
|
7 |
-
|
8 |
-
|
9 |
-
base
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
correct_answer = subtract_numbers(num1, num2, base)
|
20 |
options = [correct_answer]
|
21 |
-
|
22 |
-
# Generate
|
23 |
while len(options) < 4:
|
24 |
-
|
25 |
-
if
|
26 |
-
options.append(
|
27 |
|
28 |
random.shuffle(options)
|
29 |
|
30 |
-
question = f"
|
31 |
-
explanation = f"
|
32 |
step_by_step_solution = [
|
33 |
-
f"
|
34 |
-
"
|
35 |
-
f"
|
|
|
36 |
]
|
37 |
|
38 |
return {
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
}
|
|
|
1 |
+
import random
|
2 |
|
3 |
title = "Subtraction in Bases"
|
4 |
+
description = "This module covers subtraction operations in various bases, such as binary, octal, decimal, and hexadecimal."
|
5 |
|
6 |
+
def generate_valid_number(base, length):
|
7 |
+
if base == 16:
|
8 |
+
return ''.join(random.choices('0123456789ABCDEF', k=length))
|
9 |
+
elif base == 10:
|
10 |
+
return ''.join(random.choices('0123456789', k=length))
|
11 |
+
elif base == 8:
|
12 |
+
return ''.join(random.choices('01234567', k=length))
|
13 |
+
elif base == 2:
|
14 |
+
return ''.join(random.choices('01', k=length))
|
15 |
+
else:
|
16 |
+
raise ValueError("Unsupported base")
|
17 |
|
18 |
+
def subtract_numbers(num1, num2, base):
|
19 |
+
return bin(int(num1, base) - int(num2, base))[2:] if base == 2 else \
|
20 |
+
oct(int(num1, base) - int(num2, base))[2:] if base == 8 else \
|
21 |
+
str(int(num1, base) - int(num2, base)) if base == 10 else \
|
22 |
+
hex(int(num1, base) - int(num2, base))[2:].upper()
|
23 |
|
24 |
+
def generate_question():
|
25 |
+
base = random.choice([2, 8, 10, 16])
|
26 |
+
length = random.randint(3, 5)
|
27 |
+
|
28 |
+
num1 = generate_valid_number(base, length)
|
29 |
+
num2 = generate_valid_number(base, length)
|
30 |
+
|
31 |
correct_answer = subtract_numbers(num1, num2, base)
|
32 |
options = [correct_answer]
|
33 |
+
|
34 |
+
# Generate other random options
|
35 |
while len(options) < 4:
|
36 |
+
fake_answer = subtract_numbers(generate_valid_number(base, length), generate_valid_number(base, length), base)
|
37 |
+
if fake_answer not in options:
|
38 |
+
options.append(fake_answer)
|
39 |
|
40 |
random.shuffle(options)
|
41 |
|
42 |
+
question = f"What is the result of subtracting {num2} from {num1} in base {base}?"
|
43 |
+
explanation = f"To subtract {num2} from {num1} in base {base}, convert both numbers to base 10, perform the subtraction, and convert the result back to base {base}."
|
44 |
step_by_step_solution = [
|
45 |
+
f"1. Convert {num1} and {num2} to base 10.",
|
46 |
+
f"2. Subtract the base 10 equivalents.",
|
47 |
+
f"3. Convert the result back to base {base}.",
|
48 |
+
f"The correct answer is {correct_answer}."
|
49 |
]
|
50 |
|
51 |
return {
|
52 |
+
'question': question,
|
53 |
+
'options': options,
|
54 |
+
'correct_answer': correct_answer,
|
55 |
+
'explanation': explanation,
|
56 |
+
'step_by_step_solution': step_by_step_solution
|
57 |
}
|