File size: 1,875 Bytes
eda4dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
# modules/numbering_system/addition_bases.py

title = "Addition in Various Bases"
description = "This module covers addition operations in bases like binary, octal, and hexadecimal."

def generate_question():
    import random

    # Choose a base and corresponding valid digits
    base = random.choice([2, 8, 16])
    digits = '01' if base == 2 else '01234567' if base == 8 else '0123456789ABCDEF'

    # Generate two valid numbers for the selected base
    num1 = ''.join(random.choice(digits) for _ in range(3))
    num2 = ''.join(random.choice(digits) for _ in range(3))

    def add_numbers(num1, num2, base):
        # Perform the addition in the correct base
        return hex(int(num1, base) + int(num2, base))[2:].upper() if base == 16 else \
               oct(int(num1, base) + int(num2, base))[2:] if base == 8 else \
               bin(int(num1, base) + int(num2, base))[2:] if base == 2 else \
               str(int(num1) + int(num2))

    correct_answer = add_numbers(num1, num2, base)
    options = [correct_answer]

    # Generate incorrect answers
    while len(options) < 4:
        invalid_answer = ''.join(random.choice(digits) for _ in range(4))
        if invalid_answer != correct_answer:
            options.append(invalid_answer)
    
    random.shuffle(options)
    
    question = f"Add the numbers {num1} and {num2} in base {base}."
    explanation = f"The sum of {num1} and {num2} in base {base} is {correct_answer}."
    step_by_step_solution = [
        f"Step 1: Convert the numbers {num1} and {num2} to base 10.",
        "Step 2: Add the numbers in base 10.",
        f"Step 3: Convert the sum back to base {base}."
    ]
    
    return {
        "question": question,
        "options": options,
        "correct_answer": correct_answer,
        "explanation": explanation,
        "step_by_step_solution": step_by_step_solution
    }