|
|
|
|
|
title = "Addition in Various Bases" |
|
description = "This module covers addition operations in bases like binary, octal, and hexadecimal." |
|
|
|
def generate_question(): |
|
import random |
|
|
|
|
|
base = random.choice([2, 8, 16]) |
|
digits = '01' if base == 2 else '01234567' if base == 8 else '0123456789ABCDEF' |
|
|
|
|
|
num1 = ''.join(random.choice(digits) for _ in range(3)) |
|
num2 = ''.join(random.choice(digits) for _ in range(3)) |
|
|
|
def add_numbers(num1, num2, base): |
|
|
|
return hex(int(num1, base) + int(num2, base))[2:].upper() if base == 16 else \ |
|
oct(int(num1, base) + int(num2, base))[2:] if base == 8 else \ |
|
bin(int(num1, base) + int(num2, base))[2:] if base == 2 else \ |
|
str(int(num1) + int(num2)) |
|
|
|
correct_answer = add_numbers(num1, num2, base) |
|
options = [correct_answer] |
|
|
|
|
|
while len(options) < 4: |
|
invalid_answer = ''.join(random.choice(digits) for _ in range(4)) |
|
if invalid_answer != correct_answer: |
|
options.append(invalid_answer) |
|
|
|
random.shuffle(options) |
|
|
|
question = f"Add the numbers {num1} and {num2} in base {base}." |
|
explanation = f"The sum of {num1} and {num2} in base {base} is {correct_answer}." |
|
step_by_step_solution = [ |
|
f"Step 1: Convert the numbers {num1} and {num2} to base 10.", |
|
"Step 2: Add the numbers in base 10.", |
|
f"Step 3: Convert the sum back to base {base}." |
|
] |
|
|
|
return { |
|
"question": question, |
|
"options": options, |
|
"correct_answer": correct_answer, |
|
"explanation": explanation, |
|
"step_by_step_solution": step_by_step_solution |
|
} |
|
|