Sina Media Lab commited on
Commit
62acc4d
·
1 Parent(s): 2f94536
modules/number_system/negative_binary.py CHANGED
@@ -1,4 +1,4 @@
1
- # modules/negative_binary.py
2
 
3
  title = "Negative Binary Numbers"
4
  description = "This module covers negative binary numbers and their representation."
@@ -6,29 +6,58 @@ description = "This module covers negative binary numbers and their representati
6
  def generate_question():
7
  import random
8
 
9
- number = ''.join(random.choice('01') for _ in range(8))
 
 
 
 
10
 
11
  def calculate_negative_binary(number):
12
- # Example logic: return a simple 2's complement representation
13
  return bin(int(number, 2) * -1)[3:].zfill(len(number))
14
 
 
 
 
 
15
  correct_answer = calculate_negative_binary(number)
16
  options = [correct_answer]
17
 
18
  # Generate incorrect answers
19
  while len(options) < 4:
20
- invalid_number = ''.join(random.choice('01') for _ in range(8))
21
  if invalid_number != correct_answer:
22
  options.append(invalid_number)
23
 
24
  random.shuffle(options)
25
 
26
- question = f"What is the negative representation of the binary number {number}?"
27
- explanation = f"The negative binary representation of {number} is {correct_answer}."
28
- step_by_step_solution = [
29
- "Step 1: Determine the 2's complement of the binary number.",
30
- "Step 2: The result is the negative binary representation."
31
- ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  return {
34
  "question": question,
 
1
+ # modules/number_system/negative_binary.py
2
 
3
  title = "Negative Binary Numbers"
4
  description = "This module covers negative binary numbers and their representation."
 
6
  def generate_question():
7
  import random
8
 
9
+ # Choose a random bit length between 2 and 8 bits, inclusive
10
+ bit_length = random.randint(2, 8)
11
+
12
+ # Generate a random binary number of the selected bit length
13
+ number = ''.join(random.choice('01') for _ in range(bit_length))
14
 
15
  def calculate_negative_binary(number):
16
+ # Example logic: return the 2's complement of the number as its negative representation
17
  return bin(int(number, 2) * -1)[3:].zfill(len(number))
18
 
19
+ def calculate_decimal_value(number):
20
+ # Convert the binary number to its decimal value (considering it unsigned)
21
+ return int(number, 2)
22
+
23
  correct_answer = calculate_negative_binary(number)
24
  options = [correct_answer]
25
 
26
  # Generate incorrect answers
27
  while len(options) < 4:
28
+ invalid_number = ''.join(random.choice('01') for _ in range(bit_length))
29
  if invalid_number != correct_answer:
30
  options.append(invalid_number)
31
 
32
  random.shuffle(options)
33
 
34
+ question_type = random.choice(["negative_binary", "decimal_value"])
35
+
36
+ if question_type == "negative_binary":
37
+ question = f"What is the negative representation of the {bit_length}-bit binary number {number}?"
38
+ explanation = f"The negative binary representation of the {bit_length}-bit binary number {number} is {correct_answer}."
39
+ step_by_step_solution = [
40
+ f"Step 1: Convert the {bit_length}-bit binary number to its 2's complement.",
41
+ f"Step 2: The result is the negative binary representation."
42
+ ]
43
+ else:
44
+ decimal_value = calculate_decimal_value(number)
45
+ question = f"What is the decimal value of the {bit_length}-bit binary number {number}?"
46
+ explanation = f"The decimal value of the {bit_length}-bit binary number {number} is {decimal_value}."
47
+ step_by_step_solution = [
48
+ f"Step 1: Convert each bit of the binary number {number} to its decimal equivalent.",
49
+ f"Step 2: Sum the values considering their place values."
50
+ ]
51
+ correct_answer = str(decimal_value)
52
+ options = [correct_answer]
53
+
54
+ # Generate incorrect decimal answers
55
+ while len(options) < 4:
56
+ invalid_decimal = str(random.randint(0, 2 ** bit_length - 1))
57
+ if invalid_decimal != correct_answer:
58
+ options.append(invalid_decimal)
59
+
60
+ random.shuffle(options)
61
 
62
  return {
63
  "question": question,
modules/number_system/presentation_bases.py DELETED
@@ -1,35 +0,0 @@
1
- # modules/presentation_bases.py
2
-
3
- title = "Bases"
4
- description = "This module covers the presentation of numbers in various bases, including binary, octal, decimal, and hexadecimal."
5
-
6
- def generate_question():
7
- import random
8
- # Generate a random base
9
- base = random.choice([2, 8, 10, 16])
10
- # Generate a valid number in that base
11
- number = ''.join(random.choice('0123456789ABCDEF') for _ in range(4))
12
- # Generate incorrect answers
13
- correct_answer = number
14
- options = [correct_answer]
15
- while len(options) < 4:
16
- invalid_number = ''.join(random.choice('0123456789ABCDEF') for _ in range(4))
17
- if invalid_number != correct_answer:
18
- options.append(invalid_number)
19
- random.shuffle(options)
20
-
21
- question = f"What is the representation of {number} in base {base}?"
22
- explanation = f"The number {number} in base {base} is {correct_answer}."
23
- step_by_step_solution = [
24
- "Step 1: Identify the digits.",
25
- "Step 2: Convert each digit based on the base.",
26
- "Step 3: Combine the digits to form the final answer."
27
- ]
28
-
29
- return {
30
- "question": question,
31
- "options": options,
32
- "correct_answer": correct_answer,
33
- "explanation": explanation,
34
- "step_by_step_solution": step_by_step_solution
35
- }