math / modules /numbering_system /grouping_techniques.py
Sina Media Lab
Updates
74a6085
raw
history blame
1.95 kB
# modules/grouping_techniques.py
title = "Grouping Techniques for Conversion"
description = "This module focuses on grouping techniques for conversion between bases such as binary and hexadecimal."
def generate_question():
import random
from_base = random.choice([2, 8, 16])
to_base = 8 if from_base == 2 else 2
number = ''.join(random.choice('01') for _ in range(8))
def group_conversion(number, from_base, to_base):
# Group the binary digits for conversion
if from_base == 2 and to_base == 8:
return oct(int(number, 2))[2:]
elif from_base == 2 and to_base == 16:
return hex(int(number, 2))[2:].upper()
elif from_base == 8 and to_base == 2:
return bin(int(number, 8))[2:]
elif from_base == 16 and to_base == 2:
return bin(int(number, 16))[2:]
correct_answer = group_conversion(number, from_base, to_base)
options = [correct_answer]
# Generate incorrect answers
while len(options) < 4:
invalid_number = ''.join(random.choice('01234567') for _ in range(4))
if invalid_number != correct_answer:
options.append(invalid_number)
random.shuffle(options)
question = f"Convert the binary number {number} from base {from_base} to base {to_base} using grouping technique."
explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base} using grouping technique."
step_by_step_solution = [
"Step 1: Group the binary digits in sets of 3 (for base 8) or 4 (for base 16).",
"Step 2: Convert each group to the corresponding digit in the target base.",
"Step 3: Combine the digits to form the final answer."
]
return {
"question": question,
"options": options,
"correct_answer": correct_answer,
"explanation": explanation,
"step_by_step_solution": step_by_step_solution
}