File size: 1,342 Bytes
c9ca893
d924141
c9ca893
 
32f2e18
d924141
c9ca893
 
 
 
 
 
 
 
 
d924141
c9ca893
 
 
 
 
 
d924141
 
c9ca893
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# modules/negative_binary.py

title = "Negative Binary Numbers"
description = "This module covers negative binary numbers and their representation."

def generate_question():
    import random

    number = ''.join(random.choice('01') for _ in range(8))

    def calculate_negative_binary(number):
        # Example logic: return a simple 2's complement representation
        return bin(int(number, 2) * -1)[3:].zfill(len(number))

    correct_answer = calculate_negative_binary(number)
    options = [correct_answer]

    # Generate incorrect answers
    while len(options) < 4:
        invalid_number = ''.join(random.choice('01') for _ in range(8))
        if invalid_number != correct_answer:
            options.append(invalid_number)
    
    random.shuffle(options)
    
    question = f"What is the negative representation of the binary number {number}?"
    explanation = f"The negative binary representation of {number} is {correct_answer}."
    step_by_step_solution = [
        "Step 1: Determine the 2's complement of the binary number.",
        "Step 2: The result is the negative binary representation."
    ]
    
    return {
        "question": question,
        "options": options,
        "correct_answer": correct_answer,
        "explanation": explanation,
        "step_by_step_solution": step_by_step_solution
    }