File size: 8,771 Bytes
7ba3b4e
2a56f14
7ba3b4e
2a56f14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import streamlit as st
import random

# Module 1: Presentation in bases 2, 10, 8, and 16
def generate_question_presentation_in_bases():
    num = random.randint(1, 20)
    base = random.choice([2, 8, 10, 16])
    if base == 2:
        representation = bin(num)[2:]
    elif base == 8:
        representation = oct(num)[2:]
    elif base == 16:
        representation = hex(num)[2:]
    else:
        representation = str(num)

    question = f"What is the representation of decimal number {num} in base {base}?"
    correct_answer = representation
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(1, 20))[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = f"The number {num} in base {base} is represented as {representation}."
    return question, options, correct_answer, explanation

# Module 2: Valid or invalid numbers in each base
def generate_question_valid_or_invalid_numbers():
    base = random.choice([2, 8, 10, 16])
    length = random.randint(3, 5)
    
    def generate_valid_number():
        return ''.join([str(random.randint(0, base - 1)) for _ in range(length)])

    def generate_invalid_number():
        valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
        invalid_digit = str(random.randint(base, 9))
        return valid_digits + invalid_digit

    correct_answer = generate_invalid_number()
    options = [correct_answer]

    # Generate valid options
    while len(options) < 10:
        valid_option = generate_valid_number()
        if valid_option not in options:
            options.append(valid_option)
    
    random.shuffle(options)
    question = f"Which of the following is an invalid number in base {base}?"
    explanation = f"The number {correct_answer} is invalid in base {base} because it contains a digit outside the range 0-{base-1}."
    return question, options, correct_answer, explanation

# Module 3: Conversion with and without fractions among four bases of 10, 2, 8, and 16
def generate_question_conversion_bases():
    num = random.randint(1, 100)
    from_base = random.choice([2, 8, 10, 16])
    to_base = random.choice([2, 8, 10, 16])
    
    if from_base == 10:
        value = num
    elif from_base == 2:
        value = int(bin(num)[2:], 2)
    elif from_base == 8:
        value = int(oct(num)[2:], 8)
    else:
        value = int(hex(num)[2:], 16)
    
    if to_base == 2:
        correct_answer = bin(value)[2:]
    elif to_base == 8:
        correct_answer = oct(value)[2:]
    elif to_base == 16:
        correct_answer = hex(value)[2:]
    else:
        correct_answer = str(value)
    
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(1, 100))[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    question = f"Convert {num} from base {from_base} to base {to_base}."
    explanation = f"The number {num} in base {from_base} is {correct_answer} in base {to_base}."
    return question, options, correct_answer, explanation

# Module 4: Using grouping techniques for conversion between 2 and 8 and 16
def generate_question_grouping_techniques():
    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) < 10:
        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}."
    return question, options, correct_answer, explanation

# Module 5: Addition in base 2, 8, and 16
def generate_question_addition_in_bases():
    base = random.choice([2, 8, 16])
    num1 = random.randint(1, 15)
    num2 = random.randint(1, 15)
    
    if base == 2:
        correct_answer = bin(num1 + num2)[2:]
    elif base == 8:
        correct_answer = oct(num1 + num2)[2:]
    else:
        correct_answer = hex(num1 + num2)[2:]
    
    question = f"Add {num1} and {num2} in base {base}."
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(1, 30))[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = f"The sum of {num1} and {num2} in base {base} is {correct_answer}."
    return question, options, correct_answer, explanation

# Module 6: 2's complement questions
def generate_question_2s_complement():
    num = random.randint(-8, 7)
    bit_length = 4
    if num >= 0:
        binary = bin(num)[2:].zfill(bit_length)
    else:
        binary = bin((1 << bit_length) + num)[2:]
    
    question = f"What is the 2's complement representation of {num}?"
    correct_answer = binary
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(-8, 7) & 0xF)[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = f"The 2's complement representation of {num} is {binary}."
    return question, options, correct_answer, explanation

# Module 7: Negative binary numbers
def generate_question_negative_binary_numbers():
    num = random.randint(-8, -1)
    bit_length = 4
    binary = bin((1 << bit_length) + num)[2:]
    
    question = f"What is the binary representation of {num} using 4 bits?"
    correct_answer = binary
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(-8, -1) & 0xF)[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = f"The binary representation of {num} using 4 bits is {binary}."
    return question, options, correct_answer, explanation

# Module 8: Subtraction in base 2 using 2's complement method
def generate_question_subtraction_in_base_2():
    num1 = random.randint(1, 7)
    num2 = random.randint(1, 7)
    
    result = num1 - num2
    bit_length = 4
    if result >= 0:
        binary_result = bin(result)[2:].zfill(bit_length)
    else:
        binary_result = bin((1 << bit_length) + result)[2:]
    
    question = f"Subtract {num2} from {num1} in base 2 using 2's complement method."
    correct_answer = binary_result
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 10:
        fake_option = bin(random.randint(-7, 7) & 0xF)[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = f"The result of subtracting {num2} from {num1} in base 2 using 2's complement is {binary_result}."
    return question, options, correct_answer, explanation

# Dictionary of modules
modules = {
    "Presentation in bases 2, 10, 8, and 16": generate_question_presentation_in_bases,
    "Valid or invalid numbers in each base": generate_question_valid_or_invalid_numbers,
    "Conversion with and without fractions among four bases of 10, 2, 8, and 16": generate_question_conversion_bases,
    "Using grouping techniques for conversion between 2 and 8 and 16": generate_question_grouping_techniques,
    "Addition in base 2, 8, and 16": generate_question_addition_in_bases,
    "2's complement questions": generate_question_2s_complement,
    "Negative binary numbers": generate_question_negative_binary_numbers,
    "Subtraction in base 2 using 2's complement method": generate_question_subtraction_in_base_2,
}

# Streamlit interface
st.title("Multiple Choice Quiz")

module = st.radio("Choose a module:", list(modules.keys()))

if module:
    generate_question = modules[module]
    question, options, correct_answer, explanation = generate_question()
    
    st.write(f"**Question:** {question}")
    answer = st.radio("Choose an answer:", options)
    
    if st.button("Submit"):
        if answer == correct_answer:
            st.success("Correct!")
        else:
            st.error("Incorrect.")
        st.write(f"**Explanation:** {explanation}")

# The modular design allows for easy addition of new modules.