File size: 1,576 Bytes
d924141
 
32f2e18
 
 
d924141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

title = "Base Conversion"
description = "This module focuses on converting numbers between different bases, including binary, octal, decimal, and hexadecimal, with and without fractions."

def generate_question():
    num = random.randint(1, 100)
    from_base = random.choice([2, 8, 10, 16])
    to_base = random.choice([2, 8, 10, 16])
    
    if from_base == 10:
        value = num
    elif from_base == 2:
        value = int(bin(num)[2:], 2)
    elif from_base == 8:
        value = int(oct(num)[2:], 8)
    else:
        value = int(hex(num)[2:], 16)
    
    if to_base == 2:
        correct_answer = bin(value)[2:]
    elif to_base == 8:
        correct_answer = oct(value)[2:]
    elif to_base == 16:
        correct_answer = hex(value)[2:]
    else:
        correct_answer = str(value)
    
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 5:
        fake_option = bin(random.randint(1, 100))[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    question = f"Convert {num} from base {from_base} to base {to_base}."
    explanation = (
        f"The number {num} in base {from_base} is {correct_answer} in base {to_base}."
        "\n\n**Step-by-step solution:**\n"
        "1. Convert the number from the original base to decimal if necessary.\n"
        "2. Convert the decimal number to the target base.\n"
        "3. The correct answer is the result of the conversion."
    )
    return question, options, correct_answer, explanation