math / modules /grouping_techniques.py
Sina Media Lab
Add application file 4
32f2e18
raw
history blame
1.29 kB
import random
title = "Grouping Techniques for Base Conversion"
description = "This module covers the techniques used for converting binary numbers to octal and hexadecimal by grouping bits."
def generate_question():
binary_num = bin(random.randint(1, 63))[2:]
padded_binary = binary_num.zfill(len(binary_num) + (3 - len(binary_num) % 3) % 3)
octal = ''.join([str(int(padded_binary[i:i+3], 2)) for i in range(0, len(padded_binary), 3)])
question = f"Group the binary number {binary_num} into octal."
correct_answer = octal
options = [correct_answer]
# Generate incorrect options
while len(options) < 5:
fake_option = ''.join([str(random.randint(0, 7)) for _ in range(len(octal))])
if fake_option not in options:
options.append(fake_option)
random.shuffle(options)
explanation = (
f"The binary number {binary_num} is grouped into octal as {octal}."
"\n\n**Step-by-step solution:**\n"
"1. Pad the binary number with leading zeros to make its length a multiple of 3.\n"
"2. Group the binary digits in groups of three, starting from the right.\n"
"3. Convert each group to its octal equivalent."
)
return question, options, correct_answer, explanation