File size: 1,952 Bytes
c9ca893
d924141
c9ca893
 
32f2e18
d924141
c9ca893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d924141
c9ca893
 
 
 
 
 
d924141
 
c9ca893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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
    }