File size: 2,231 Bytes
26f260d c9ca893 26f260d d924141 26f260d 984a89c 26f260d 984a89c 26f260d c9ca893 26f260d c9ca893 26f260d c9ca893 26f260d c9ca893 26f260d c9ca893 26f260d 984a89c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import random
title = "Subtraction in Bases"
description = "This module covers subtraction operations in various bases, such as binary, octal, decimal, and hexadecimal."
def generate_valid_number(base, length):
if base == 16:
return ''.join(random.choices('0123456789ABCDEF', k=length))
elif base == 10:
return ''.join(random.choices('0123456789', k=length))
elif base == 8:
return ''.join(random.choices('01234567', k=length))
elif base == 2:
return ''.join(random.choices('01', k=length))
else:
raise ValueError("Unsupported base")
def subtract_numbers(num1, num2, base):
return bin(int(num1, base) - int(num2, base))[2:] if base == 2 else \
oct(int(num1, base) - int(num2, base))[2:] if base == 8 else \
str(int(num1, base) - int(num2, base)) if base == 10 else \
hex(int(num1, base) - int(num2, base))[2:].upper()
def generate_question():
base = random.choice([2, 8, 10, 16])
length = random.randint(3, 5)
num1 = generate_valid_number(base, length)
num2 = generate_valid_number(base, length)
correct_answer = subtract_numbers(num1, num2, base)
options = [correct_answer]
# Generate other random options
while len(options) < 4:
fake_answer = subtract_numbers(generate_valid_number(base, length), generate_valid_number(base, length), base)
if fake_answer not in options:
options.append(fake_answer)
random.shuffle(options)
question = f"What is the result of subtracting {num2} from {num1} in base {base}?"
explanation = f"To subtract {num2} from {num1} in base {base}, convert both numbers to base 10, perform the subtraction, and convert the result back to base {base}."
step_by_step_solution = [
f"1. Convert {num1} and {num2} to base 10.",
f"2. Subtract the base 10 equivalents.",
f"3. Convert the result back to base {base}.",
f"The correct answer is {correct_answer}."
]
return {
'question': question,
'options': options,
'correct_answer': correct_answer,
'explanation': explanation,
'step_by_step_solution': step_by_step_solution
}
|