Sina Media Lab
commited on
Commit
·
9c228bf
1
Parent(s):
018bb0b
Updates
Browse files
modules/number_system/grouping_techniques.py
CHANGED
@@ -46,7 +46,7 @@ def generate_question():
|
|
46 |
elif from_base == 8 and to_base == 2:
|
47 |
# Convert octal to binary
|
48 |
integer_part, *fraction_part = number.split('.')
|
49 |
-
integer_result = bin(int(
|
50 |
if fraction_part:
|
51 |
fraction_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in fraction_part[0]])
|
52 |
return f"{integer_result}.{fraction_result}"
|
@@ -54,7 +54,7 @@ def generate_question():
|
|
54 |
elif from_base == 16 and to_base == 2:
|
55 |
# Convert hexadecimal to binary
|
56 |
integer_part, *fraction_part = number.split('.')
|
57 |
-
integer_result = bin(int(
|
58 |
if fraction_part:
|
59 |
fraction_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in fraction_part[0]])
|
60 |
return f"{integer_result}.{fraction_result}"
|
@@ -65,15 +65,17 @@ def generate_question():
|
|
65 |
|
66 |
# Generate incorrect answers ensuring they are unique and valid
|
67 |
while len(options) < 4:
|
68 |
-
if
|
69 |
-
invalid_number = ''.join(random.choice('
|
70 |
-
elif
|
71 |
-
invalid_number = ''.join(random.choice('
|
72 |
-
|
73 |
-
invalid_number = ''.join(random.choice('
|
74 |
|
75 |
-
|
76 |
-
|
|
|
|
|
77 |
|
78 |
options = list(options)
|
79 |
random.shuffle(options)
|
|
|
46 |
elif from_base == 8 and to_base == 2:
|
47 |
# Convert octal to binary
|
48 |
integer_part, *fraction_part = number.split('.')
|
49 |
+
integer_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in integer_part])
|
50 |
if fraction_part:
|
51 |
fraction_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in fraction_part[0]])
|
52 |
return f"{integer_result}.{fraction_result}"
|
|
|
54 |
elif from_base == 16 and to_base == 2:
|
55 |
# Convert hexadecimal to binary
|
56 |
integer_part, *fraction_part = number.split('.')
|
57 |
+
integer_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in integer_part])
|
58 |
if fraction_part:
|
59 |
fraction_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in fraction_part[0]])
|
60 |
return f"{integer_result}.{fraction_result}"
|
|
|
65 |
|
66 |
# Generate incorrect answers ensuring they are unique and valid
|
67 |
while len(options) < 4:
|
68 |
+
if from_base == 2:
|
69 |
+
invalid_number = ''.join(random.choice('01') for _ in range(len(number.replace('.', ''))))
|
70 |
+
elif from_base == 8:
|
71 |
+
invalid_number = ''.join(random.choice('01234567') for _ in range(len(number.replace('.', ''))))
|
72 |
+
elif from_base == 16:
|
73 |
+
invalid_number = ''.join(random.choice('0123456789ABCDEF') for _ in range(len(number.replace('.', ''))))
|
74 |
|
75 |
+
# Generate the corresponding incorrect conversion
|
76 |
+
invalid_converted_number = group_conversion(invalid_number, from_base, to_base)
|
77 |
+
if invalid_converted_number != correct_answer:
|
78 |
+
options.add(invalid_converted_number)
|
79 |
|
80 |
options = list(options)
|
81 |
random.shuffle(options)
|