Sina Media Lab commited on
Commit
32f2e18
·
1 Parent(s): d924141

Add application file 4

Browse files
app.py CHANGED
@@ -1,38 +1,71 @@
1
  import streamlit as st
2
  import importlib
3
 
4
- # List of available modules
5
  module_names = {
6
- "Presentation in bases 2, 10, 8, and 16": "presentation_bases",
7
- "Valid or invalid numbers in each base": "valid_invalid_numbers",
8
- "Conversion with and without fractions among four bases of 10, 2, 8, and 16": "conversion_bases",
9
- "Using grouping techniques for conversion between 2 and 8 and 16": "grouping_techniques",
10
- "Addition in base 2, 8, and 16": "addition_bases",
11
- "2's complement questions": "twos_complement",
12
- "Negative binary numbers": "negative_binary",
13
- "Subtraction in base 2 using 2's complement method": "subtraction_bases",
14
  }
15
 
 
 
 
 
 
 
 
 
16
  # Streamlit interface
17
- st.sidebar.title("Multiple Choice Quiz")
18
- module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()))
19
 
20
  if module_name:
21
  module_file = module_names[module_name]
22
  try:
23
  module = importlib.import_module(f'modules.{module_file}')
24
  generate_question = module.generate_question
25
- question, options, correct_answer, explanation = generate_question()
 
 
 
 
 
26
 
 
 
 
 
 
27
  st.write(f"**Question:** {question}")
28
  answer = st.radio("Choose an answer:", options)
29
-
 
30
  if st.button("Submit"):
 
31
  if answer == correct_answer:
 
32
  st.success("Correct!", icon="✅")
33
- st.write(f"**Explanation:** {explanation}")
34
  else:
35
  st.error("Incorrect.", icon="❌")
36
- st.write(f"**Explanation:** {explanation}")
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  except ModuleNotFoundError:
38
  st.error(f"The module '{module_name}' was not found. Please select another module.")
 
1
  import streamlit as st
2
  import importlib
3
 
4
+ # List of available modules with shorter names and icons
5
  module_names = {
6
+ "Bases": "presentation_bases",
7
+ "Validity": "valid_invalid_numbers",
8
+ "Conversion": "conversion_bases",
9
+ "Grouping": "grouping_techniques",
10
+ "Addition": "addition_bases",
11
+ "2's Complement": "twos_complement",
12
+ "Negative Numbers": "negative_binary",
13
+ "Subtraction": "subtraction_bases",
14
  }
15
 
16
+ # Initialize session state for tracking answers and progress
17
+ if 'correct_count' not in st.session_state:
18
+ st.session_state.correct_count = 0
19
+ if 'question_count' not in st.session_state:
20
+ st.session_state.question_count = 0
21
+ if 'current_module' not in st.session_state:
22
+ st.session_state.current_module = None
23
+
24
  # Streamlit interface
25
+ st.sidebar.title("Quiz Modules")
26
+ module_name = st.sidebar.radio("Choose a module:", list(module_names.keys()), index=0)
27
 
28
  if module_name:
29
  module_file = module_names[module_name]
30
  try:
31
  module = importlib.import_module(f'modules.{module_file}')
32
  generate_question = module.generate_question
33
+ title = getattr(module, 'title', f"{module_name} Module")
34
+ description = getattr(module, 'description', "Description is not available")
35
+
36
+ # Show module title and description
37
+ st.title(title)
38
+ st.write(description)
39
 
40
+ # Add spacing to ensure question visibility on Hugging Face
41
+ st.markdown("<br><br>", unsafe_allow_html=True)
42
+
43
+ # Generate and display the question
44
+ question, options, correct_answer, explanation = generate_question()
45
  st.write(f"**Question:** {question}")
46
  answer = st.radio("Choose an answer:", options)
47
+
48
+ # Track user's response
49
  if st.button("Submit"):
50
+ st.session_state.question_count += 1
51
  if answer == correct_answer:
52
+ st.session_state.correct_count += 1
53
  st.success("Correct!", icon="✅")
 
54
  else:
55
  st.error("Incorrect.", icon="❌")
56
+ st.write(f"**Explanation:** {explanation}")
57
+
58
+ # Option to go to the next or previous question
59
+ col1, col2 = st.columns(2)
60
+ if col1.button("Previous Question"):
61
+ st.experimental_rerun() # To simulate going back (if history is managed)
62
+ if col2.button("Next Question"):
63
+ st.experimental_rerun() # To simulate moving forward
64
+
65
+ # Show percentage of correct answers
66
+ if st.session_state.question_count > 0:
67
+ correct_percentage = (st.session_state.correct_count / st.session_state.question_count) * 100
68
+ st.write(f"**Correct Answers:** {st.session_state.correct_count}/{st.session_state.question_count} ({correct_percentage:.2f}%)")
69
+
70
  except ModuleNotFoundError:
71
  st.error(f"The module '{module_name}' was not found. Please select another module.")
modules/addition_bases.py CHANGED
@@ -1,43 +1,35 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
- num = random.randint(1, 100)
5
- from_base = random.choice([2, 8, 10, 16])
6
- to_base = random.choice([2, 8, 10, 16])
7
-
8
- if from_base == 10:
9
- value = num
10
- elif from_base == 2:
11
- value = int(bin(num)[2:], 2)
12
- elif from_base == 8:
13
- value = int(oct(num)[2:], 8)
14
- else:
15
- value = int(hex(num)[2:], 16)
16
 
17
- if to_base == 2:
18
- correct_answer = bin(value)[2:]
19
- elif to_base == 8:
20
- correct_answer = oct(value)[2:]
21
- elif to_base == 16:
22
- correct_answer = hex(value)[2:]
23
  else:
24
- correct_answer = str(value)
25
 
 
26
  options = [correct_answer]
27
 
28
  # Generate incorrect options
29
  while len(options) < 5:
30
- fake_option = bin(random.randint(1, 100))[2:]
31
  if fake_option not in options:
32
  options.append(fake_option)
33
 
34
  random.shuffle(options)
35
- question = f"Convert {num} from base {from_base} to base {to_base}."
36
  explanation = (
37
- f"The number {num} in base {from_base} is {correct_answer} in base {to_base}."
38
  "\n\n**Step-by-step solution:**\n"
39
- "1. Convert the number from the original base to decimal if necessary.\n"
40
- "2. Convert the decimal number to the target base.\n"
41
- "3. The correct answer is the result of the conversion."
42
  )
43
  return question, options, correct_answer, explanation
 
1
  import random
2
 
3
+ title = "Addition in Different Bases"
4
+ description = "This module helps you practice adding numbers in different bases such as binary, octal, and hexadecimal."
5
+
6
  def generate_question():
7
+ base = random.choice([2, 8, 16])
8
+ num1 = random.randint(1, 15)
9
+ num2 = random.randint(1, 15)
 
 
 
 
 
 
 
 
 
10
 
11
+ if base == 2:
12
+ correct_answer = bin(num1 + num2)[2:]
13
+ elif base == 8:
14
+ correct_answer = oct(num1 + num2)[2:]
 
 
15
  else:
16
+ correct_answer = hex(num1 + num2)[2:]
17
 
18
+ question = f"Add {num1} and {num2} in base {base}."
19
  options = [correct_answer]
20
 
21
  # Generate incorrect options
22
  while len(options) < 5:
23
+ fake_option = bin(random.randint(1, 30))[2:]
24
  if fake_option not in options:
25
  options.append(fake_option)
26
 
27
  random.shuffle(options)
 
28
  explanation = (
29
+ f"The sum of {num1} and {num2} in base {base} is {correct_answer}."
30
  "\n\n**Step-by-step solution:**\n"
31
+ "1. Convert the numbers to decimal if necessary.\n"
32
+ "2. Perform the addition in decimal.\n"
33
+ "3. Convert the result back to the base if needed."
34
  )
35
  return question, options, correct_answer, explanation
modules/conversion_bases.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  num = random.randint(1, 100)
5
  from_base = random.choice([2, 8, 10, 16])
 
1
  import random
2
 
3
+ title = "Base Conversion"
4
+ description = "This module focuses on converting numbers between different bases, including binary, octal, decimal, and hexadecimal, with and without fractions."
5
+
6
  def generate_question():
7
  num = random.randint(1, 100)
8
  from_base = random.choice([2, 8, 10, 16])
modules/grouping_techniques.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  binary_num = bin(random.randint(1, 63))[2:]
5
  padded_binary = binary_num.zfill(len(binary_num) + (3 - len(binary_num) % 3) % 3)
 
1
  import random
2
 
3
+ title = "Grouping Techniques for Base Conversion"
4
+ description = "This module covers the techniques used for converting binary numbers to octal and hexadecimal by grouping bits."
5
+
6
  def generate_question():
7
  binary_num = bin(random.randint(1, 63))[2:]
8
  padded_binary = binary_num.zfill(len(binary_num) + (3 - len(binary_num) % 3) % 3)
modules/negative_binary.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  num = random.randint(-8, -1)
5
  bit_length = 4
 
1
  import random
2
 
3
+ title = "Negative Numbers in Binary"
4
+ description = "This module focuses on the binary representation of negative numbers using 2's complement and other methods."
5
+
6
  def generate_question():
7
  num = random.randint(-8, -1)
8
  bit_length = 4
modules/presentation_bases.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  num = random.randint(1, 20)
5
  base = random.choice([2, 8, 10, 16])
 
1
  import random
2
 
3
+ title = "Presentation in Bases"
4
+ description = "This module covers the representation of numbers in different bases including binary, octal, decimal, and hexadecimal."
5
+
6
  def generate_question():
7
  num = random.randint(1, 20)
8
  base = random.choice([2, 8, 10, 16])
modules/subtraction_bases.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  num1 = random.randint(1, 7)
5
  num2 = random.randint(1, 7)
 
1
  import random
2
 
3
+ title = "Subtraction in Base 2 Using 2's Complement"
4
+ description = "This module teaches subtraction in binary using the 2's complement method, which allows subtraction to be performed as an addition operation."
5
+
6
  def generate_question():
7
  num1 = random.randint(1, 7)
8
  num2 = random.randint(1, 7)
modules/twos_complement.py CHANGED
@@ -1,43 +1,32 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
- num = random.randint(1, 100)
5
- from_base = random.choice([2, 8, 10, 16])
6
- to_base = random.choice([2, 8, 10, 16])
7
-
8
- if from_base == 10:
9
- value = num
10
- elif from_base == 2:
11
- value = int(bin(num)[2:], 2)
12
- elif from_base == 8:
13
- value = int(oct(num)[2:], 8)
14
- else:
15
- value = int(hex(num)[2:], 16)
16
-
17
- if to_base == 2:
18
- correct_answer = bin(value)[2:]
19
- elif to_base == 8:
20
- correct_answer = oct(value)[2:]
21
- elif to_base == 16:
22
- correct_answer = hex(value)[2:]
23
  else:
24
- correct_answer = str(value)
25
 
 
 
26
  options = [correct_answer]
27
 
28
  # Generate incorrect options
29
  while len(options) < 5:
30
- fake_option = bin(random.randint(1, 100))[2:]
31
  if fake_option not in options:
32
  options.append(fake_option)
33
 
34
  random.shuffle(options)
35
- question = f"Convert {num} from base {from_base} to base {to_base}."
36
  explanation = (
37
- f"The number {num} in base {from_base} is {correct_answer} in base {to_base}."
38
  "\n\n**Step-by-step solution:**\n"
39
- "1. Convert the number from the original base to decimal if necessary.\n"
40
- "2. Convert the decimal number to the target base.\n"
41
- "3. The correct answer is the result of the conversion."
42
  )
43
  return question, options, correct_answer, explanation
 
1
  import random
2
 
3
+ title = "2's Complement"
4
+ description = "This module covers the representation of numbers using the 2's complement method, which is commonly used for representing negative numbers in binary."
5
+
6
  def generate_question():
7
+ num = random.randint(-8, 7)
8
+ bit_length = 4
9
+ if num >= 0:
10
+ binary = bin(num)[2:].zfill(bit_length)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  else:
12
+ binary = bin((1 << bit_length) + num)[2:]
13
 
14
+ question = f"What is the 2's complement representation of {num}?"
15
+ correct_answer = binary
16
  options = [correct_answer]
17
 
18
  # Generate incorrect options
19
  while len(options) < 5:
20
+ fake_option = bin(random.randint(-8, 7) & 0xF)[2:]
21
  if fake_option not in options:
22
  options.append(fake_option)
23
 
24
  random.shuffle(options)
 
25
  explanation = (
26
+ f"The 2's complement representation of {num} is {binary}."
27
  "\n\n**Step-by-step solution:**\n"
28
+ "1. If the number is positive, convert it to binary and pad with leading zeros to fit the bit length.\n"
29
+ "2. If the number is negative, take its absolute value, convert to binary, invert the digits, and add 1.\n"
30
+ "3. The result is the 2's complement representation."
31
  )
32
  return question, options, correct_answer, explanation
modules/valid_invalid_numbers.py CHANGED
@@ -1,5 +1,8 @@
1
  import random
2
 
 
 
 
3
  def generate_question():
4
  base = random.choice([2, 8, 10, 16])
5
  length = random.randint(3, 5)
 
1
  import random
2
 
3
+ title = "Validity of Numbers in Bases"
4
+ description = "This module helps you determine whether a given number is valid or invalid in various bases like binary, octal, decimal, and hexadecimal."
5
+
6
  def generate_question():
7
  base = random.choice([2, 8, 10, 16])
8
  length = random.randint(3, 5)