Sina Media Lab
commited on
Commit
·
018bb0b
1
Parent(s):
e29cc8c
Updates
Browse files
modules/number_system/grouping_techniques.py
CHANGED
|
@@ -9,7 +9,6 @@ def generate_question():
|
|
| 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
|
|
@@ -64,9 +63,16 @@ def generate_question():
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
options.add(invalid_number)
|
| 71 |
|
| 72 |
options = list(options)
|
|
|
|
| 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 |
|
| 13 |
if from_base == 2:
|
| 14 |
# Generate a random binary number, optionally with a fractional part
|
|
|
|
| 63 |
correct_answer = group_conversion(number, from_base, to_base)
|
| 64 |
options = {correct_answer}
|
| 65 |
|
| 66 |
+
# Generate incorrect answers ensuring they are unique and valid
|
| 67 |
while len(options) < 4:
|
| 68 |
+
if to_base == 8:
|
| 69 |
+
invalid_number = ''.join(random.choice('01234567') for _ in range(len(correct_answer.replace('.', ''))))
|
| 70 |
+
elif to_base == 16:
|
| 71 |
+
invalid_number = ''.join(random.choice('0123456789ABCDEF') for _ in range(len(correct_answer.replace('.', ''))))
|
| 72 |
+
else:
|
| 73 |
+
invalid_number = ''.join(random.choice('01') for _ in range(len(correct_answer.replace('.', ''))))
|
| 74 |
+
|
| 75 |
+
invalid_number = group_conversion(invalid_number, from_base, to_base)
|
| 76 |
options.add(invalid_number)
|
| 77 |
|
| 78 |
options = list(options)
|
modules/number_system/negative_binary.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import random
|
| 2 |
|
| 3 |
title = "Negative Binary Numbers"
|
|
@@ -33,14 +35,15 @@ def generate_question():
|
|
| 33 |
correct_decimal = calculate_negative_binary(number)
|
| 34 |
correct_binary = decimal_to_binary(correct_decimal, num_bits)
|
| 35 |
|
| 36 |
-
options =
|
| 37 |
|
| 38 |
# Generate incorrect answers
|
| 39 |
while len(options) < 4:
|
| 40 |
random_decimal = random.randint(-2**(num_bits-1), 2**(num_bits-1)-1)
|
| 41 |
if random_decimal != correct_decimal:
|
| 42 |
-
options.
|
| 43 |
|
|
|
|
| 44 |
random.shuffle(options)
|
| 45 |
|
| 46 |
question = f"What is the decimal value of the signed {num_bits}-bit binary number {number}?"
|
|
|
|
| 1 |
+
# modules/number_system/negative_binary.py
|
| 2 |
+
|
| 3 |
import random
|
| 4 |
|
| 5 |
title = "Negative Binary Numbers"
|
|
|
|
| 35 |
correct_decimal = calculate_negative_binary(number)
|
| 36 |
correct_binary = decimal_to_binary(correct_decimal, num_bits)
|
| 37 |
|
| 38 |
+
options = {correct_decimal} # Using a set to ensure unique options
|
| 39 |
|
| 40 |
# Generate incorrect answers
|
| 41 |
while len(options) < 4:
|
| 42 |
random_decimal = random.randint(-2**(num_bits-1), 2**(num_bits-1)-1)
|
| 43 |
if random_decimal != correct_decimal:
|
| 44 |
+
options.add(random_decimal) # Add to the set to avoid duplicates
|
| 45 |
|
| 46 |
+
options = list(options) # Convert the set to a list
|
| 47 |
random.shuffle(options)
|
| 48 |
|
| 49 |
question = f"What is the decimal value of the signed {num_bits}-bit binary number {number}?"
|