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