Sina Media Lab
commited on
Commit
·
40d3dfc
1
Parent(s):
5f6edac
Updates
Browse files
modules/number_system/grouping_techniques.py
CHANGED
|
@@ -10,20 +10,25 @@ def generate_question():
|
|
| 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
|
| 15 |
number = ''.join(random.choice('01') for _ in range(random.randint(4, 8)))
|
| 16 |
if random.choice([True, False]):
|
|
|
|
| 17 |
number += '.' + ''.join(random.choice('01') for _ in range(random.randint(1, 4)))
|
| 18 |
elif from_base == 8:
|
| 19 |
# Generate a random octal number, optionally with a fractional part
|
| 20 |
number = ''.join(random.choice('01234567') for _ in range(random.randint(2, 4)))
|
| 21 |
if random.choice([True, False]):
|
|
|
|
| 22 |
number += '.' + ''.join(random.choice('01234567') for _ in range(random.randint(1, 3)))
|
| 23 |
elif from_base == 16:
|
| 24 |
# Generate a random hexadecimal number, optionally with a fractional part
|
| 25 |
number = ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(2, 4)))
|
| 26 |
if random.choice([True, False]):
|
|
|
|
| 27 |
number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(1, 3)))
|
| 28 |
|
| 29 |
def group_conversion(number, from_base, to_base):
|
|
@@ -33,7 +38,7 @@ def generate_question():
|
|
| 33 |
integer_result = oct(int(integer_part, 2))[2:]
|
| 34 |
if fraction_part:
|
| 35 |
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)])
|
| 36 |
-
return f"{integer_result}.{fraction_result}"
|
| 37 |
return integer_result
|
| 38 |
elif from_base == 2 and to_base == 16:
|
| 39 |
# Convert binary to hexadecimal
|
|
@@ -41,7 +46,7 @@ def generate_question():
|
|
| 41 |
integer_result = hex(int(integer_part, 2))[2:].upper()
|
| 42 |
if fraction_part:
|
| 43 |
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)])
|
| 44 |
-
return f"{integer_result}.{fraction_result}"
|
| 45 |
return integer_result
|
| 46 |
elif from_base == 8 and to_base == 2:
|
| 47 |
# Convert octal to binary
|
|
@@ -49,7 +54,7 @@ def generate_question():
|
|
| 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}"
|
| 53 |
return integer_result
|
| 54 |
elif from_base == 16 and to_base == 2:
|
| 55 |
# Convert hexadecimal to binary
|
|
@@ -57,7 +62,7 @@ def generate_question():
|
|
| 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}"
|
| 61 |
return integer_result
|
| 62 |
|
| 63 |
correct_answer = group_conversion(number, from_base, to_base)
|
|
@@ -67,10 +72,16 @@ def generate_question():
|
|
| 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)
|
|
|
|
| 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 |
+
has_fraction = False # Track if the number includes a fraction
|
| 14 |
+
|
| 15 |
if from_base == 2:
|
| 16 |
# Generate a random binary number, optionally with a fractional part
|
| 17 |
number = ''.join(random.choice('01') for _ in range(random.randint(4, 8)))
|
| 18 |
if random.choice([True, False]):
|
| 19 |
+
has_fraction = True
|
| 20 |
number += '.' + ''.join(random.choice('01') for _ in range(random.randint(1, 4)))
|
| 21 |
elif from_base == 8:
|
| 22 |
# Generate a random octal number, optionally with a fractional part
|
| 23 |
number = ''.join(random.choice('01234567') for _ in range(random.randint(2, 4)))
|
| 24 |
if random.choice([True, False]):
|
| 25 |
+
has_fraction = True
|
| 26 |
number += '.' + ''.join(random.choice('01234567') for _ in range(random.randint(1, 3)))
|
| 27 |
elif from_base == 16:
|
| 28 |
# Generate a random hexadecimal number, optionally with a fractional part
|
| 29 |
number = ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(2, 4)))
|
| 30 |
if random.choice([True, False]):
|
| 31 |
+
has_fraction = True
|
| 32 |
number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(1, 3)))
|
| 33 |
|
| 34 |
def group_conversion(number, from_base, to_base):
|
|
|
|
| 38 |
integer_result = oct(int(integer_part, 2))[2:]
|
| 39 |
if fraction_part:
|
| 40 |
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)])
|
| 41 |
+
return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
|
| 42 |
return integer_result
|
| 43 |
elif from_base == 2 and to_base == 16:
|
| 44 |
# Convert binary to hexadecimal
|
|
|
|
| 46 |
integer_result = hex(int(integer_part, 2))[2:].upper()
|
| 47 |
if fraction_part:
|
| 48 |
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)])
|
| 49 |
+
return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
|
| 50 |
return integer_result
|
| 51 |
elif from_base == 8 and to_base == 2:
|
| 52 |
# Convert octal to binary
|
|
|
|
| 54 |
integer_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in integer_part])
|
| 55 |
if fraction_part:
|
| 56 |
fraction_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in fraction_part[0]])
|
| 57 |
+
return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
|
| 58 |
return integer_result
|
| 59 |
elif from_base == 16 and to_base == 2:
|
| 60 |
# Convert hexadecimal to binary
|
|
|
|
| 62 |
integer_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in integer_part])
|
| 63 |
if fraction_part:
|
| 64 |
fraction_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in fraction_part[0]])
|
| 65 |
+
return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
|
| 66 |
return integer_result
|
| 67 |
|
| 68 |
correct_answer = group_conversion(number, from_base, to_base)
|
|
|
|
| 72 |
while len(options) < 4:
|
| 73 |
if from_base == 2:
|
| 74 |
invalid_number = ''.join(random.choice('01') for _ in range(len(number.replace('.', ''))))
|
| 75 |
+
if has_fraction:
|
| 76 |
+
invalid_number += '.' + ''.join(random.choice('01') for _ in range(len(number.split('.')[1])))
|
| 77 |
elif from_base == 8:
|
| 78 |
invalid_number = ''.join(random.choice('01234567') for _ in range(len(number.replace('.', ''))))
|
| 79 |
+
if has_fraction:
|
| 80 |
+
invalid_number += '.' + ''.join(random.choice('01234567') for _ in range(len(number.split('.')[1])))
|
| 81 |
elif from_base == 16:
|
| 82 |
invalid_number = ''.join(random.choice('0123456789ABCDEF') for _ in range(len(number.replace('.', ''))))
|
| 83 |
+
if has_fraction:
|
| 84 |
+
invalid_number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(len(number.split('.')[1])))
|
| 85 |
|
| 86 |
# Generate the corresponding incorrect conversion
|
| 87 |
invalid_converted_number = group_conversion(invalid_number, from_base, to_base)
|