File size: 2,143 Bytes
c9ca893
 
 
 
d924141
 
c9ca893
984a89c
f59ba06
c9ca893
 
f59ba06
 
 
 
 
984a89c
c9ca893
 
 
 
 
 
 
 
 
 
 
 
984a89c
c9ca893
 
984a89c
c9ca893
 
f59ba06
 
 
 
c9ca893
 
 
 
 
 
 
 
 
 
 
 
 
 
984a89c
c9ca893
 
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
48
49
50
51
52
53
54
55
56
57
58
# modules/conversion_bases.py

title = "Conversion Between Bases"
description = "This module covers conversion techniques between different bases, including with and without fractions."

def generate_question():
    import random

    # Choose a base and corresponding valid digits
    from_base = random.choice([2, 8, 10, 16])
    to_base = random.choice([2, 8, 10, 16])

    digits = '01' if from_base == 2 else '01234567' if from_base == 8 else '0123456789' if from_base == 10 else '0123456789ABCDEF'

    # Generate a valid number for the selected from_base
    number = ''.join(random.choice(digits) for _ in range(4))

    def convert_number(number, from_base, to_base):
        # Convert from `from_base` to base 10
        base_10 = int(number, from_base)
        # Convert from base 10 to `to_base`
        if to_base == 10:
            return str(base_10)
        elif to_base == 16:
            return hex(base_10)[2:].upper()
        elif to_base == 8:
            return oct(base_10)[2:]
        elif to_base == 2:
            return bin(base_10)[2:]

    correct_answer = convert_number(number, from_base, to_base)
    options = [correct_answer]

    # Generate incorrect answers
    while len(options) < 4:
        invalid_number = ''.join(random.choice(digits) for _ in range(4))
        invalid_answer = convert_number(invalid_number, from_base, to_base)
        if invalid_answer != correct_answer:
            options.append(invalid_answer)
    
    random.shuffle(options)
    
    question = f"Convert the number {number} from base {from_base} to base {to_base}."
    explanation = f"The number {number} in base {from_base} is {correct_answer} in base {to_base}."
    step_by_step_solution = [
        f"Step 1: Convert the number {number} from base {from_base} to base 10.",
        f"Step 2: Convert the base 10 number to base {to_base}.",
        f"Step 3: The correct answer is {correct_answer}."
    ]
    
    return {
        "question": question,
        "options": options,
        "correct_answer": correct_answer,
        "explanation": explanation,
        "step_by_step_solution": step_by_step_solution
    }