|
import random |
|
|
|
title = "Base Conversion" |
|
description = "This module focuses on converting numbers between different bases, including binary, octal, decimal, and hexadecimal, with and without fractions." |
|
|
|
def generate_question(): |
|
num = random.randint(1, 100) |
|
from_base = random.choice([2, 8, 10, 16]) |
|
to_base = random.choice([2, 8, 10, 16]) |
|
|
|
if from_base == 10: |
|
value = num |
|
elif from_base == 2: |
|
value = int(bin(num)[2:], 2) |
|
elif from_base == 8: |
|
value = int(oct(num)[2:], 8) |
|
else: |
|
value = int(hex(num)[2:], 16) |
|
|
|
if to_base == 2: |
|
correct_answer = bin(value)[2:] |
|
elif to_base == 8: |
|
correct_answer = oct(value)[2:] |
|
elif to_base == 16: |
|
correct_answer = hex(value)[2:] |
|
else: |
|
correct_answer = str(value) |
|
|
|
options = [correct_answer] |
|
|
|
|
|
while len(options) < 5: |
|
fake_option = bin(random.randint(1, 100))[2:] |
|
if fake_option not in options: |
|
options.append(fake_option) |
|
|
|
random.shuffle(options) |
|
question = f"Convert {num} from base {from_base} to base {to_base}." |
|
explanation = ( |
|
f"The number {num} in base {from_base} is {correct_answer} in base {to_base}." |
|
"\n\n**Step-by-step solution:**\n" |
|
"1. Convert the number from the original base to decimal if necessary.\n" |
|
"2. Convert the decimal number to the target base.\n" |
|
"3. The correct answer is the result of the conversion." |
|
) |
|
return question, options, correct_answer, explanation |
|
|