|
|
|
|
|
title = "Subtraction in Bases" |
|
description = "This module focuses on subtraction operations in binary using the 2's complement method." |
|
|
|
def generate_question(): |
|
import random |
|
|
|
base = random.choice([2, 8, 16]) |
|
num1 = ''.join(random.choice('0123456789ABCDEF') for _ in range(3)) |
|
num2 = ''.join(random.choice('0123456789ABCDEF') for _ in range(3)) |
|
|
|
def subtract_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 = subtract_numbers(num1, num2, base) |
|
options = [correct_answer] |
|
|
|
|
|
while len(options) < 4: |
|
invalid_answer = ''.join(random.choice('0123456789ABCDEF') for _ in range(4)) |
|
if invalid_answer != correct_answer: |
|
options.append(invalid_answer) |
|
|
|
random.shuffle(options) |
|
|
|
question = f"Subtract the number {num2} from {num1} in base {base}." |
|
explanation = f"The result of subtracting {num2} from {num1} in base {base} is {correct_answer}." |
|
step_by_step_solution = [ |
|
f"Step 1: Convert the numbers {num1} and {num2} to base 10.", |
|
"Step 2: Subtract the numbers in base 10.", |
|
f"Step 3: Convert the result back to base {base}." |
|
] |
|
|
|
return { |
|
"question": question, |
|
"options": options, |
|
"correct_answer": correct_answer, |
|
"explanation": explanation, |
|
"step_by_step_solution": step_by_step_solution |
|
} |
|
|