File size: 1,486 Bytes
d924141
 
 
984a89c
32f2e18
 
984a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32f2e18
984a89c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import random

def generate_question():
    base = random.choice([2, 8, 10, 16])
    num1 = random.randint(1, 15)
    num2 = random.randint(1, 15)

    if base == 10:
        num1_rep = str(num1)
        num2_rep = str(num2)
    elif base == 2:
        num1_rep = bin(num1)[2:]
        num2_rep = bin(num2)[2:]
    elif base == 8:
        num1_rep = oct(num1)[2:]
        num2_rep = oct(num2)[2:]
    elif base == 16:
        num1_rep = hex(num1)[2:].upper()
        num2_rep = hex(num2)[2:].upper()

    sum_decimal = num1 + num2
    if base == 10:
        correct_answer = str(sum_decimal)
    elif base == 2:
        correct_answer = bin(sum_decimal)[2:]
    elif base == 8:
        correct_answer = oct(sum_decimal)[2:]
    elif base == 16:
        correct_answer = hex(sum_decimal)[2:].upper()

    steps = [
        f"Convert {num1_rep} and {num2_rep} to decimal if needed.",
        f"Add the numbers: {num1} + {num2} = {sum_decimal} in decimal.",
        f"Convert {sum_decimal} from decimal to base {base}.",
        f"The result in base {base} is {correct_answer}."
    ]

    question_data = {
        "question": f"Add the numbers {num1_rep} and {num2_rep} in base {base}.",
        "options": [correct_answer] + [str(random.randint(1, 30)) for _ in range(3)],
        "correct_answer": correct_answer,
        "explanation": f"The sum of {num1_rep} and {num2_rep} in base {base} is {correct_answer}.",
        "step_by_step_solution": steps
    }

    return question_data