Sina Media Lab
commited on
Commit
·
e29cc8c
1
Parent(s):
3540df9
Updates
Browse files
modules/number_system/grouping_techniques.py
CHANGED
@@ -1,41 +1,81 @@
|
|
1 |
-
# modules/grouping_techniques.py
|
2 |
|
3 |
title = "Grouping Techniques for Conversion"
|
4 |
-
description = "This module focuses on grouping techniques for conversion between bases such as binary and hexadecimal."
|
5 |
|
6 |
def generate_question():
|
7 |
import random
|
8 |
|
|
|
9 |
from_base = random.choice([2, 8, 16])
|
10 |
-
to_base = 8 if from_base == 2 else 2
|
11 |
-
number = ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def group_conversion(number, from_base, to_base):
|
14 |
-
# Group the binary digits for conversion
|
15 |
if from_base == 2 and to_base == 8:
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
elif from_base == 2 and to_base == 16:
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
elif from_base == 8 and to_base == 2:
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
elif from_base == 16 and to_base == 2:
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
correct_answer = group_conversion(number, from_base, to_base)
|
25 |
-
options =
|
26 |
|
27 |
-
# Generate incorrect answers
|
28 |
while len(options) < 4:
|
29 |
-
invalid_number = ''.join(random.choice('
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
random.shuffle(options)
|
34 |
|
35 |
-
question = f"Convert the
|
36 |
-
explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base} using grouping technique."
|
37 |
step_by_step_solution = [
|
38 |
-
"Step 1: Group the
|
39 |
"Step 2: Convert each group to the corresponding digit in the target base.",
|
40 |
"Step 3: Combine the digits to form the final answer."
|
41 |
]
|
|
|
1 |
+
# modules/number_system/grouping_techniques.py
|
2 |
|
3 |
title = "Grouping Techniques for Conversion"
|
4 |
+
description = "This module focuses on grouping techniques for conversion between bases such as binary, octal, and hexadecimal."
|
5 |
|
6 |
def generate_question():
|
7 |
import random
|
8 |
|
9 |
+
# Choose a random base to convert from, either 2, 8, or 16
|
10 |
from_base = random.choice([2, 8, 16])
|
11 |
+
to_base = 8 if from_base == 2 else 2 if from_base == 8 else 2
|
12 |
+
number = ''
|
13 |
+
|
14 |
+
if from_base == 2:
|
15 |
+
# Generate a random binary number, optionally with a fractional part
|
16 |
+
number = ''.join(random.choice('01') for _ in range(random.randint(4, 8)))
|
17 |
+
if random.choice([True, False]):
|
18 |
+
number += '.' + ''.join(random.choice('01') for _ in range(random.randint(1, 4)))
|
19 |
+
elif from_base == 8:
|
20 |
+
# Generate a random octal number, optionally with a fractional part
|
21 |
+
number = ''.join(random.choice('01234567') for _ in range(random.randint(2, 4)))
|
22 |
+
if random.choice([True, False]):
|
23 |
+
number += '.' + ''.join(random.choice('01234567') for _ in range(random.randint(1, 3)))
|
24 |
+
elif from_base == 16:
|
25 |
+
# Generate a random hexadecimal number, optionally with a fractional part
|
26 |
+
number = ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(2, 4)))
|
27 |
+
if random.choice([True, False]):
|
28 |
+
number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(1, 3)))
|
29 |
|
30 |
def group_conversion(number, from_base, to_base):
|
|
|
31 |
if from_base == 2 and to_base == 8:
|
32 |
+
# Convert binary to octal
|
33 |
+
integer_part, *fraction_part = number.split('.')
|
34 |
+
integer_result = oct(int(integer_part, 2))[2:]
|
35 |
+
if fraction_part:
|
36 |
+
fraction_result = ''.join([oct(int(f'{fraction_part[0][i:i+3]:0<3}', 2))[2:] for i in range(0, len(fraction_part[0]), 3)])
|
37 |
+
return f"{integer_result}.{fraction_result}"
|
38 |
+
return integer_result
|
39 |
elif from_base == 2 and to_base == 16:
|
40 |
+
# Convert binary to hexadecimal
|
41 |
+
integer_part, *fraction_part = number.split('.')
|
42 |
+
integer_result = hex(int(integer_part, 2))[2:].upper()
|
43 |
+
if fraction_part:
|
44 |
+
fraction_result = ''.join([hex(int(f'{fraction_part[0][i:i+4]:0<4}', 2))[2:].upper() for i in range(0, len(fraction_part[0]), 4)])
|
45 |
+
return f"{integer_result}.{fraction_result}"
|
46 |
+
return integer_result
|
47 |
elif from_base == 8 and to_base == 2:
|
48 |
+
# Convert octal to binary
|
49 |
+
integer_part, *fraction_part = number.split('.')
|
50 |
+
integer_result = bin(int(integer_part, 8))[2:]
|
51 |
+
if fraction_part:
|
52 |
+
fraction_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in fraction_part[0]])
|
53 |
+
return f"{integer_result}.{fraction_result}"
|
54 |
+
return integer_result
|
55 |
elif from_base == 16 and to_base == 2:
|
56 |
+
# Convert hexadecimal to binary
|
57 |
+
integer_part, *fraction_part = number.split('.')
|
58 |
+
integer_result = bin(int(integer_part, 16))[2:]
|
59 |
+
if fraction_part:
|
60 |
+
fraction_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in fraction_part[0]])
|
61 |
+
return f"{integer_result}.{fraction_result}"
|
62 |
+
return integer_result
|
63 |
|
64 |
correct_answer = group_conversion(number, from_base, to_base)
|
65 |
+
options = {correct_answer}
|
66 |
|
67 |
+
# Generate incorrect answers ensuring they are unique
|
68 |
while len(options) < 4:
|
69 |
+
invalid_number = group_conversion(''.join(random.choice('0123456789ABCDEF') for _ in range(len(correct_answer))), from_base, to_base)
|
70 |
+
options.add(invalid_number)
|
71 |
+
|
72 |
+
options = list(options)
|
73 |
random.shuffle(options)
|
74 |
|
75 |
+
question = f"Convert the number {number} from base {from_base} to base {to_base} using grouping technique."
|
76 |
+
explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base} using the grouping technique."
|
77 |
step_by_step_solution = [
|
78 |
+
"Step 1: Group the digits of the number according to the conversion rules.",
|
79 |
"Step 2: Convert each group to the corresponding digit in the target base.",
|
80 |
"Step 3: Combine the digits to form the final answer."
|
81 |
]
|