Sina Media Lab commited on
Commit
62b8133
·
1 Parent(s): 26f260d
Files changed (2) hide show
  1. modules/twos_complement.py +22 -9
  2. requirements.txt +2 -1
modules/twos_complement.py CHANGED
@@ -1,20 +1,23 @@
1
- # modules/twos_complement.py
 
2
 
3
  title = "2's Complement Questions"
4
  description = "This module explains the 2's complement method for representing negative numbers."
5
 
6
  def generate_question():
7
- import random
8
-
9
  number = ''.join(random.choice('01') for _ in range(8))
10
 
11
  def calculate_twos_complement(number):
12
- n = len(number)
13
  ones_complement = ''.join('1' if bit == '0' else '0' for bit in number)
 
 
14
  twos_complement = bin(int(ones_complement, 2) + 1)[2:]
15
- return twos_complement.zfill(n)
 
 
16
 
17
- correct_answer = calculate_twos_complement(number)
18
  options = [correct_answer]
19
 
20
  # Generate incorrect answers
@@ -26,12 +29,22 @@ def generate_question():
26
  random.shuffle(options)
27
 
28
  question = f"What is the 2's complement of the binary number {number}?"
29
- explanation = f"The 2's complement of {number} is {correct_answer}."
 
 
 
 
 
30
  step_by_step_solution = [
31
- "Step 1: Find the 1's complement by flipping all bits.",
32
- "Step 2: Add 1 to the 1's complement to get the 2's complement."
 
 
 
33
  ]
34
 
 
 
35
  return {
36
  "question": question,
37
  "options": options,
 
1
+ import random
2
+ import sympy as sp
3
 
4
  title = "2's Complement Questions"
5
  description = "This module explains the 2's complement method for representing negative numbers."
6
 
7
  def generate_question():
 
 
8
  number = ''.join(random.choice('01') for _ in range(8))
9
 
10
  def calculate_twos_complement(number):
11
+ # Calculate 1's complement
12
  ones_complement = ''.join('1' if bit == '0' else '0' for bit in number)
13
+
14
+ # Calculate 2's complement
15
  twos_complement = bin(int(ones_complement, 2) + 1)[2:]
16
+ twos_complement = twos_complement.zfill(len(number))
17
+
18
+ return ones_complement, twos_complement
19
 
20
+ ones_complement, correct_answer = calculate_twos_complement(number)
21
  options = [correct_answer]
22
 
23
  # Generate incorrect answers
 
29
  random.shuffle(options)
30
 
31
  question = f"What is the 2's complement of the binary number {number}?"
32
+
33
+ # Generate a step-by-step solution using SymPy
34
+ num_expr = sp.sympify(f'0b{number}')
35
+ ones_expr = sp.sympify(f'0b{ones_complement}')
36
+ twos_expr = ones_expr + 1
37
+
38
  step_by_step_solution = [
39
+ f"Step 1: Start with the original binary number: {number}",
40
+ f"Step 2: Find the 1's complement by flipping all bits: {ones_complement}",
41
+ f"Step 3: Add 1 to the 1's complement:",
42
+ f" {ones_complement} + 1 = {bin(twos_expr)[2:].zfill(len(number))}",
43
+ f"Step 4: The 2's complement of {number} is {correct_answer}."
44
  ]
45
 
46
+ explanation = f"The 2's complement of {number} is {correct_answer}. It is calculated by inverting the bits and adding 1."
47
+
48
  return {
49
  "question": question,
50
  "options": options,
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  streamlit
2
  huggingface_hub
3
- fpdf
 
 
1
  streamlit
2
  huggingface_hub
3
+ fpdf
4
+ sympy