|
import random |
|
|
|
def generate_question(): |
|
base = random.choice([2, 8, 10, 16]) |
|
num1 = random.randint(1, 15) |
|
num2 = random.randint(1, 15) |
|
|
|
if base == 10: |
|
num1_rep = str(num1) |
|
num2_rep = str(num2) |
|
elif base == 2: |
|
num1_rep = bin(num1)[2:] |
|
num2_rep = bin(num2)[2:] |
|
elif base == 8: |
|
num1_rep = oct(num1)[2:] |
|
num2_rep = oct(num2)[2:] |
|
elif base == 16: |
|
num1_rep = hex(num1)[2:].upper() |
|
num2_rep = hex(num2)[2:].upper() |
|
|
|
sum_decimal = num1 + num2 |
|
if base == 10: |
|
correct_answer = str(sum_decimal) |
|
elif base == 2: |
|
correct_answer = bin(sum_decimal)[2:] |
|
elif base == 8: |
|
correct_answer = oct(sum_decimal)[2:] |
|
elif base == 16: |
|
correct_answer = hex(sum_decimal)[2:].upper() |
|
|
|
steps = [ |
|
f"Convert {num1_rep} and {num2_rep} to decimal if needed.", |
|
f"Add the numbers: {num1} + {num2} = {sum_decimal} in decimal.", |
|
f"Convert {sum_decimal} from decimal to base {base}.", |
|
f"The result in base {base} is {correct_answer}." |
|
] |
|
|
|
question_data = { |
|
"question": f"Add the numbers {num1_rep} and {num2_rep} in base {base}.", |
|
"options": [correct_answer] + [str(random.randint(1, 30)) for _ in range(3)], |
|
"correct_answer": correct_answer, |
|
"explanation": f"The sum of {num1_rep} and {num2_rep} in base {base} is {correct_answer}.", |
|
"step_by_step_solution": steps |
|
} |
|
|
|
return question_data |
|
|