Sina Media Lab commited on
Commit
07ba05d
·
1 Parent(s): 06735cd
Files changed (2) hide show
  1. app.py +10 -15
  2. modules/valid_invalid_numbers.py +18 -8
app.py CHANGED
@@ -3,7 +3,6 @@ import importlib
3
  import logging
4
  from fpdf import FPDF
5
  import uuid
6
- import json
7
 
8
  # Configure logging
9
  logging.basicConfig(level=logging.INFO)
@@ -144,11 +143,16 @@ def get_option_style(option):
144
  return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
145
  return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
146
 
 
 
 
 
147
  selected_answer = st.radio(
148
- "Choose an answer:",
149
- current_question['options'],
150
- index=current_question['options'].index(current_question.get('selected', '')) if current_question.get('selected') else None,
151
- key=st.session_state.current_index
 
152
  )
153
 
154
  if st.button("Submit"):
@@ -161,13 +165,4 @@ if st.button("Submit"):
161
  st.session_state.correct_count += 1
162
  st.session_state.module_correct_count[module_name] += 1
163
 
164
- # Retain and highlight the options after submission
165
- for option in current_question['options']:
166
- st.markdown(f"<div style='{get_option_style(option)}'>{option}</div>", unsafe_allow_html=True)
167
-
168
- # Show explanation and step-by-step solution
169
- if current_question.get('answered', False):
170
- st.write(f"**Explanation:** {current_question['explanation']}")
171
- st.write("**Step-by-Step Solution:**")
172
- for step in current_question['step_by_step_solution']:
173
- st.write(step)
 
3
  import logging
4
  from fpdf import FPDF
5
  import uuid
 
6
 
7
  # Configure logging
8
  logging.basicConfig(level=logging.INFO)
 
143
  return "background-color:#f8d7da;padding:10px;border-radius:5px;" # Red background for incorrect
144
  return "background-color:#f0f0f0;padding:10px;border-radius:5px;" # Gray background by default
145
 
146
+ def format_option(option, option_style):
147
+ return f"<label style='{option_style}'>{option}</label>"
148
+
149
+ # Display options with custom formatting
150
  selected_answer = st.radio(
151
+ "Choose an answer:",
152
+ options=[format_option(option, get_option_style(option)) for option in current_question['options']],
153
+ format_func=lambda x: x,
154
+ key=st.session_state.current_index,
155
+ label_visibility="collapsed",
156
  )
157
 
158
  if st.button("Submit"):
 
165
  st.session_state.correct_count += 1
166
  st.session_state.module_correct_count[module_name] += 1
167
 
168
+ # Since we're not adding an additional section, remove any content below the options.
 
 
 
 
 
 
 
 
 
modules/valid_invalid_numbers.py CHANGED
@@ -8,23 +8,33 @@ def generate_question():
8
  return ''.join([str(random.randint(0, base - 1)) for _ in range(length)])
9
 
10
  def generate_invalid_number():
11
- valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
12
- invalid_digit = str(random.randint(base, 9))
13
- return valid_digits + invalid_digit
 
 
 
 
 
 
 
14
 
15
  correct_answer = generate_invalid_number()
16
  options = [correct_answer] + [generate_valid_number() for _ in range(3)]
 
 
 
 
 
 
 
17
 
18
  question_data = {
19
  "question": f"Which of the following is an invalid number in base {base}?",
20
  "options": options,
21
  "correct_answer": correct_answer,
22
  "explanation": f"The number {correct_answer} is invalid in base {base} because it contains a digit outside the range 0-{base-1}.",
23
- "step_by_step_solution": [
24
- "1. Identify the valid digits for the base.",
25
- f"2. Check each option to see if it contains any invalid digits (greater than {base-1}).",
26
- "3. The correct answer will have a digit that is not allowed in the specified base."
27
- ]
28
  }
29
 
30
  return question_data
 
8
  return ''.join([str(random.randint(0, base - 1)) for _ in range(length)])
9
 
10
  def generate_invalid_number():
11
+ if base <= 9:
12
+ valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
13
+ invalid_digit = str(random.randint(base, 9))
14
+ return valid_digits + invalid_digit
15
+ else:
16
+ # For bases higher than 9 (base 10 and base 16), generate an invalid number differently.
17
+ valid_digits = [str(random.randint(0, base - 1)) for _ in range(length - 1)]
18
+ invalid_digit = chr(random.randint(65, 70)) # Use A-F for invalid hex digits
19
+ valid_digits[random.randint(0, length - 2)] = invalid_digit
20
+ return ''.join(valid_digits)
21
 
22
  correct_answer = generate_invalid_number()
23
  options = [correct_answer] + [generate_valid_number() for _ in range(3)]
24
+ random.shuffle(options)
25
+
26
+ steps = [
27
+ f"Check if all digits are within the valid range for base {base}.",
28
+ f"In base {base}, valid digits are 0 to {base-1}.",
29
+ f"The number {correct_answer} contains a digit outside this range, so it is invalid."
30
+ ]
31
 
32
  question_data = {
33
  "question": f"Which of the following is an invalid number in base {base}?",
34
  "options": options,
35
  "correct_answer": correct_answer,
36
  "explanation": f"The number {correct_answer} is invalid in base {base} because it contains a digit outside the range 0-{base-1}.",
37
+ "step_by_step_solution": steps
 
 
 
 
38
  }
39
 
40
  return question_data