File size: 2,489 Bytes
bc85220 c9ca893 d924141 bc85220 f59ba06 984a89c c9ca893 4c02307 ff3311b c9ca893 ff3311b 984a89c c9ca893 984a89c c9ca893 bc85220 f59ba06 bc85220 f59ba06 c9ca893 bc85220 1f50ad3 ff3311b c9ca893 bc85220 ff3311b bc85220 4c02307 bc85220 c9ca893 984a89c c9ca893 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 59 60 61 |
import random
import sympy as sp
title = "Conversion Between Bases"
description = "This module covers conversion techniques between different bases, including with and without fractions."
def generate_question():
# Choose a base and corresponding valid digits, ensuring from_base and to_base are not the same
from_base = random.choice([2, 8, 10, 16])
to_base = random.choice([b for b in [2, 8, 10, 16] if b != from_base])
digits = '01' if from_base == 2 else '01234567' if from_base == 8 else '0123456789' if from_base == 10 else '0123456789ABCDEF'
# Generate a valid number for the selected from_base
number = ''.join(random.choice(digits) for _ in range(4))
def convert_number(number, from_base, to_base):
# Convert from `from_base` to base 10 using Python's built-in `int`
base_10 = int(number, from_base)
# Convert from base 10 to `to_base` using Python's built-in methods
if to_base == 10:
return str(base_10)
elif to_base == 16:
return hex(base_10)[2:].upper()
elif to_base == 8:
return oct(base_10)[2:]
elif to_base == 2:
return bin(base_10)[2:]
correct_answer = convert_number(number, from_base, to_base)
options = [correct_answer]
# Generate incorrect answers
while len(options) < 4:
invalid_number = ''.join(random.choice(digits) for _ in range(4))
invalid_answer = convert_number(invalid_number, from_base, to_base)
if invalid_answer not in options:
options.append(invalid_answer)
random.shuffle(options)
question = f"Convert the number {number} from base {from_base} to base {to_base}."
explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base}."
# Step-by-step solution using SymPy for demonstration
step_by_step_solution = [
f"Step 1: Convert the number {number} from base {from_base} to base 10:",
f" {number} (base {from_base}) = {sp.Integer(base_10)} (base 10).",
f"Step 2: Convert the base 10 number to base {to_base}:",
f" {base_10} (base 10) = {correct_answer} (base {to_base}).",
f"Step 3: 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
}
|