Sina Media Lab commited on
Commit
26f260d
·
1 Parent(s): c7df340
Files changed (1) hide show
  1. modules/subtraction_bases.py +41 -28
modules/subtraction_bases.py CHANGED
@@ -1,44 +1,57 @@
1
- # modules/subtraction_bases.py
2
 
3
  title = "Subtraction in Bases"
4
- description = "This module focuses on subtraction operations in binary using the 2's complement method."
5
 
6
- def generate_question():
7
- import random
8
-
9
- base = random.choice([2, 8, 16])
10
- num1 = ''.join(random.choice('0123456789ABCDEF') for _ in range(3))
11
- num2 = ''.join(random.choice('0123456789ABCDEF') for _ in range(3))
 
 
 
 
 
12
 
13
- def subtract_numbers(num1, num2, base):
14
- return hex(int(num1, base) - int(num2, base))[2:].upper() if base == 16 else \
15
- oct(int(num1, base) - int(num2, base))[2:] if base == 8 else \
16
- bin(int(num1, base) - int(num2, base))[2:] if base == 2 else \
17
- str(int(num1) - int(num2))
18
 
 
 
 
 
 
 
 
19
  correct_answer = subtract_numbers(num1, num2, base)
20
  options = [correct_answer]
21
-
22
- # Generate incorrect answers
23
  while len(options) < 4:
24
- invalid_answer = ''.join(random.choice('0123456789ABCDEF') for _ in range(4))
25
- if invalid_answer != correct_answer:
26
- options.append(invalid_answer)
27
 
28
  random.shuffle(options)
29
 
30
- question = f"Subtract the number {num2} from {num1} in base {base}."
31
- explanation = f"The result of subtracting {num2} from {num1} in base {base} is {correct_answer}."
32
  step_by_step_solution = [
33
- f"Step 1: Convert the numbers {num1} and {num2} to base 10.",
34
- "Step 2: Subtract the numbers in base 10.",
35
- f"Step 3: Convert the result back to base {base}."
 
36
  ]
37
 
38
  return {
39
- "question": question,
40
- "options": options,
41
- "correct_answer": correct_answer,
42
- "explanation": explanation,
43
- "step_by_step_solution": step_by_step_solution
44
  }
 
1
+ import random
2
 
3
  title = "Subtraction in Bases"
4
+ description = "This module covers subtraction operations in various bases, such as binary, octal, decimal, and hexadecimal."
5
 
6
+ def generate_valid_number(base, length):
7
+ if base == 16:
8
+ return ''.join(random.choices('0123456789ABCDEF', k=length))
9
+ elif base == 10:
10
+ return ''.join(random.choices('0123456789', k=length))
11
+ elif base == 8:
12
+ return ''.join(random.choices('01234567', k=length))
13
+ elif base == 2:
14
+ return ''.join(random.choices('01', k=length))
15
+ else:
16
+ raise ValueError("Unsupported base")
17
 
18
+ def subtract_numbers(num1, num2, base):
19
+ return bin(int(num1, base) - int(num2, base))[2:] if base == 2 else \
20
+ oct(int(num1, base) - int(num2, base))[2:] if base == 8 else \
21
+ str(int(num1, base) - int(num2, base)) if base == 10 else \
22
+ hex(int(num1, base) - int(num2, base))[2:].upper()
23
 
24
+ def generate_question():
25
+ base = random.choice([2, 8, 10, 16])
26
+ length = random.randint(3, 5)
27
+
28
+ num1 = generate_valid_number(base, length)
29
+ num2 = generate_valid_number(base, length)
30
+
31
  correct_answer = subtract_numbers(num1, num2, base)
32
  options = [correct_answer]
33
+
34
+ # Generate other random options
35
  while len(options) < 4:
36
+ fake_answer = subtract_numbers(generate_valid_number(base, length), generate_valid_number(base, length), base)
37
+ if fake_answer not in options:
38
+ options.append(fake_answer)
39
 
40
  random.shuffle(options)
41
 
42
+ question = f"What is the result of subtracting {num2} from {num1} in base {base}?"
43
+ explanation = f"To subtract {num2} from {num1} in base {base}, convert both numbers to base 10, perform the subtraction, and convert the result back to base {base}."
44
  step_by_step_solution = [
45
+ f"1. Convert {num1} and {num2} to base 10.",
46
+ f"2. Subtract the base 10 equivalents.",
47
+ f"3. Convert the result back to base {base}.",
48
+ f"The correct answer is {correct_answer}."
49
  ]
50
 
51
  return {
52
+ 'question': question,
53
+ 'options': options,
54
+ 'correct_answer': correct_answer,
55
+ 'explanation': explanation,
56
+ 'step_by_step_solution': step_by_step_solution
57
  }