File size: 5,818 Bytes
e29cc8c
d924141
c9ca893
e29cc8c
32f2e18
d924141
c9ca893
 
e29cc8c
c9ca893
e29cc8c
 
40d3dfc
 
e29cc8c
 
 
 
40d3dfc
e29cc8c
 
 
 
 
40d3dfc
e29cc8c
 
 
 
 
40d3dfc
e29cc8c
c9ca893
 
 
e29cc8c
 
 
 
 
40d3dfc
e29cc8c
c9ca893
e29cc8c
 
 
 
 
40d3dfc
e29cc8c
c9ca893
e29cc8c
 
9c228bf
e29cc8c
 
40d3dfc
e29cc8c
c9ca893
e29cc8c
 
9c228bf
e29cc8c
 
40d3dfc
e29cc8c
c9ca893
 
e29cc8c
c9ca893
018bb0b
c9ca893
9c228bf
 
40d3dfc
 
9c228bf
 
40d3dfc
 
9c228bf
 
40d3dfc
 
018bb0b
9c228bf
 
 
 
e29cc8c
 
d924141
c9ca893
e29cc8c
 
c9ca893
e29cc8c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# modules/number_system/grouping_techniques.py

title = "Grouping Techniques for Conversion"
description = "This module focuses on grouping techniques for conversion between bases such as binary, octal, and hexadecimal."

def generate_question():
    import random

    # Choose a random base to convert from, either 2, 8, or 16
    from_base = random.choice([2, 8, 16])
    to_base = 8 if from_base == 2 else 2 if from_base == 8 else 2

    has_fraction = False  # Track if the number includes a fraction

    if from_base == 2:
        # Generate a random binary number, optionally with a fractional part
        number = ''.join(random.choice('01') for _ in range(random.randint(4, 8)))
        if random.choice([True, False]):
            has_fraction = True
            number += '.' + ''.join(random.choice('01') for _ in range(random.randint(1, 4)))
    elif from_base == 8:
        # Generate a random octal number, optionally with a fractional part
        number = ''.join(random.choice('01234567') for _ in range(random.randint(2, 4)))
        if random.choice([True, False]):
            has_fraction = True
            number += '.' + ''.join(random.choice('01234567') for _ in range(random.randint(1, 3)))
    elif from_base == 16:
        # Generate a random hexadecimal number, optionally with a fractional part
        number = ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(2, 4)))
        if random.choice([True, False]):
            has_fraction = True
            number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(random.randint(1, 3)))

    def group_conversion(number, from_base, to_base):
        if from_base == 2 and to_base == 8:
            # Convert binary to octal
            integer_part, *fraction_part = number.split('.')
            integer_result = oct(int(integer_part, 2))[2:]
            if fraction_part:
                fraction_result = ''.join([oct(int(f'{fraction_part[0][i:i+3]:0<3}', 2))[2:] for i in range(0, len(fraction_part[0]), 3)])
                return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
            return integer_result
        elif from_base == 2 and to_base == 16:
            # Convert binary to hexadecimal
            integer_part, *fraction_part = number.split('.')
            integer_result = hex(int(integer_part, 2))[2:].upper()
            if fraction_part:
                fraction_result = ''.join([hex(int(f'{fraction_part[0][i:i+4]:0<4}', 2))[2:].upper() for i in range(0, len(fraction_part[0]), 4)])
                return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
            return integer_result
        elif from_base == 8 and to_base == 2:
            # Convert octal to binary
            integer_part, *fraction_part = number.split('.')
            integer_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in integer_part])
            if fraction_part:
                fraction_result = ''.join([bin(int(digit, 8))[2:].zfill(3) for digit in fraction_part[0]])
                return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
            return integer_result
        elif from_base == 16 and to_base == 2:
            # Convert hexadecimal to binary
            integer_part, *fraction_part = number.split('.')
            integer_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in integer_part])
            if fraction_part:
                fraction_result = ''.join([bin(int(digit, 16))[2:].zfill(4) for digit in fraction_part[0]])
                return f"{integer_result}.{fraction_result}" if has_fraction else integer_result
            return integer_result

    correct_answer = group_conversion(number, from_base, to_base)
    options = {correct_answer}

    # Generate incorrect answers ensuring they are unique and valid
    while len(options) < 4:
        if from_base == 2:
            invalid_number = ''.join(random.choice('01') for _ in range(len(number.replace('.', ''))))
            if has_fraction:
                invalid_number += '.' + ''.join(random.choice('01') for _ in range(len(number.split('.')[1])))
        elif from_base == 8:
            invalid_number = ''.join(random.choice('01234567') for _ in range(len(number.replace('.', ''))))
            if has_fraction:
                invalid_number += '.' + ''.join(random.choice('01234567') for _ in range(len(number.split('.')[1])))
        elif from_base == 16:
            invalid_number = ''.join(random.choice('0123456789ABCDEF') for _ in range(len(number.replace('.', ''))))
            if has_fraction:
                invalid_number += '.' + ''.join(random.choice('0123456789ABCDEF') for _ in range(len(number.split('.')[1])))
        
        # Generate the corresponding incorrect conversion
        invalid_converted_number = group_conversion(invalid_number, from_base, to_base)
        if invalid_converted_number != correct_answer:
            options.add(invalid_converted_number)

    options = list(options)
    random.shuffle(options)
    
    question = f"Convert the 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 the grouping technique."
    step_by_step_solution = [
        "Step 1: Group the digits of the number according to the conversion rules.",
        "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
    }