File size: 1,277 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
import random

title = "Presentation in Bases"
description = "This module covers the representation of numbers in different bases including binary, octal, decimal, and hexadecimal."

def generate_question():
    num = random.randint(1, 20)
    base = random.choice([2, 8, 10, 16])
    if base == 2:
        representation = bin(num)[2:]
    elif base == 8:
        representation = oct(num)[2:]
    elif base == 16:
        representation = hex(num)[2:]
    else:
        representation = str(num)

    question = f"What is the representation of decimal number {num} in base {base}?"
    correct_answer = representation
    options = [correct_answer]
    
    # Generate incorrect options
    while len(options) < 5:
        fake_option = bin(random.randint(1, 20))[2:]
        if fake_option not in options:
            options.append(fake_option)
    
    random.shuffle(options)
    explanation = (
        f"The number {num} in base {base} is represented as {representation}."
        "\n\n**Step-by-step solution:**\n"
        "1. Convert the number from decimal to the required base.\n"
        "2. Match with the provided options.\n"
        "3. The correct answer is the one that matches the conversion."
    )
    return question, options, correct_answer, explanation