math / app.py
Sina Media Lab
Add application file 2
2a56f14
raw
history blame
8.77 kB
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.