File size: 1,286 Bytes
d924141
 
32f2e18
 
 
d924141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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