Sina Media Lab commited on
Commit
1f50ad3
·
1 Parent(s): bbc7ac2
Files changed (2) hide show
  1. app.py +13 -13
  2. modules/conversion_bases.py +18 -15
app.py CHANGED
@@ -223,20 +223,20 @@ def display_question_with_styles(question_data):
223
  </div>
224
  """, unsafe_allow_html=True)
225
 
226
- for option in question_data['options']:
227
- option_class = ''
228
- if option == question_data['correct_answer']:
229
- option_class = 'correct'
230
- elif option == question_data['selected']:
231
- option_class = 'incorrect'
232
-
233
- st.markdown(f"""
234
- <div class="option {option_class}">
235
- {option}
236
- </div>
237
- """, unsafe_allow_html=True)
238
-
239
  if question_data.get('answered', False):
 
 
 
 
 
 
 
 
 
 
 
 
 
240
  st.markdown(f"""
241
  <div class="explanation-box">
242
  <strong>Explanation:</strong> {question_data['explanation']}
 
223
  </div>
224
  """, unsafe_allow_html=True)
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  if question_data.get('answered', False):
227
+ for option in question_data['options']:
228
+ option_class = ''
229
+ if option == question_data['correct_answer']:
230
+ option_class = 'correct'
231
+ elif option == question_data['selected']:
232
+ option_class = 'incorrect'
233
+
234
+ st.markdown(f"""
235
+ <div class="option {option_class}">
236
+ {option}
237
+ </div>
238
+ """, unsafe_allow_html=True)
239
+
240
  st.markdown(f"""
241
  <div class="explanation-box">
242
  <strong>Explanation:</strong> {question_data['explanation']}
modules/conversion_bases.py CHANGED
@@ -5,10 +5,10 @@ description = "This module covers conversion techniques between different bases,
5
 
6
  def generate_question():
7
  import random
8
-
9
- # Choose a base and corresponding valid digits
10
- from_base = random.choice([2, 8, 10, 16])
11
- to_base = random.choice([2, 8, 10, 16])
12
 
13
  digits = '01' if from_base == 2 else '01234567' if from_base == 8 else '0123456789' if from_base == 10 else '0123456789ABCDEF'
14
 
@@ -16,38 +16,41 @@ def generate_question():
16
  number = ''.join(random.choice(digits) for _ in range(4))
17
 
18
  def convert_number(number, from_base, to_base):
19
- # Convert from `from_base` to base 10
20
- base_10 = int(number, from_base)
21
  # Convert from base 10 to `to_base`
22
  if to_base == 10:
23
  return str(base_10)
24
  elif to_base == 16:
25
- return hex(base_10)[2:].upper()
26
  elif to_base == 8:
27
- return oct(base_10)[2:]
28
  elif to_base == 2:
29
- return bin(base_10)[2:]
30
 
31
  correct_answer = convert_number(number, from_base, to_base)
32
  options = [correct_answer]
33
 
34
  # Generate incorrect answers
35
- while len(options) < 4:
36
  invalid_number = ''.join(random.choice(digits) for _ in range(4))
37
  invalid_answer = convert_number(invalid_number, from_base, to_base)
38
- if invalid_answer != correct_answer:
39
  options.append(invalid_answer)
40
 
41
  random.shuffle(options)
42
 
43
  question = f"Convert the number {number} from base {from_base} to base {to_base}."
44
- explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base}."
45
  step_by_step_solution = [
46
- f"Step 1: Convert the number {number} from base {from_base} to base 10.",
47
- f"Step 2: Convert the base 10 number to base {to_base}.",
48
- f"Step 3: The correct answer is {correct_answer}."
 
49
  ]
50
 
 
 
51
  return {
52
  "question": question,
53
  "options": options,
 
5
 
6
  def generate_question():
7
  import random
8
+ from sympy import Integer
9
+
10
+ # Choose distinct bases for conversion
11
+ from_base, to_base = random.sample([2, 8, 10, 16], 2)
12
 
13
  digits = '01' if from_base == 2 else '01234567' if from_base == 8 else '0123456789' if from_base == 10 else '0123456789ABCDEF'
14
 
 
16
  number = ''.join(random.choice(digits) for _ in range(4))
17
 
18
  def convert_number(number, from_base, to_base):
19
+ # Convert from `from_base` to base 10 using SymPy
20
+ base_10 = Integer(number, from_base)
21
  # Convert from base 10 to `to_base`
22
  if to_base == 10:
23
  return str(base_10)
24
  elif to_base == 16:
25
+ return base_10.hex().upper()
26
  elif to_base == 8:
27
+ return base_10.oct()
28
  elif to_base == 2:
29
+ return base_10.bin()
30
 
31
  correct_answer = convert_number(number, from_base, to_base)
32
  options = [correct_answer]
33
 
34
  # Generate incorrect answers
35
+ while len(options) < 5:
36
  invalid_number = ''.join(random.choice(digits) for _ in range(4))
37
  invalid_answer = convert_number(invalid_number, from_base, to_base)
38
+ if invalid_answer != correct_answer and invalid_answer not in options:
39
  options.append(invalid_answer)
40
 
41
  random.shuffle(options)
42
 
43
  question = f"Convert the number {number} from base {from_base} to base {to_base}."
44
+
45
  step_by_step_solution = [
46
+ f"Step 1: Interpret the number {number} as a base-{from_base} integer.",
47
+ f"Step 2: Convert this base-{from_base} number to its base-10 equivalent: {Integer(number, from_base)}.",
48
+ f"Step 3: Convert the base-10 equivalent to base {to_base}: {correct_answer}.",
49
+ f"Step 4: Therefore, the correct answer is {correct_answer}."
50
  ]
51
 
52
+ explanation = " ".join(step_by_step_solution)
53
+
54
  return {
55
  "question": question,
56
  "options": options,