File size: 1,505 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 = "Validity of Numbers in Bases"
description = "This module helps you determine whether a given number is valid or invalid in various bases like binary, octal, decimal, and hexadecimal."

def generate_question():
    base = random.choice([2, 8, 10, 16])
    length = random.randint(3, 5)
    
    def generate_valid_number():
        return ''.join([str(random.randint(0, base - 1)) for _ in range(length)])

    def generate_invalid_number():
        valid_digits = ''.join([str(random.randint(0, base - 1)) for _ in range(length - 1)])
        invalid_digit = str(random.randint(base, 9))
        return valid_digits + invalid_digit

    correct_answer = generate_invalid_number()
    options = [correct_answer]

    # Generate valid options
    while len(options) < 5:
        valid_option = generate_valid_number()
        if valid_option not in options:
            options.append(valid_option)
    
    random.shuffle(options)
    question = f"Which of the following is an invalid number in base {base}?"
    explanation = (
        f"The number {correct_answer} is invalid in base {base} because it contains a digit outside the range 0-{base-1}."
        "\n\n**Step-by-step solution:**\n"
        "1. Identify the valid digits for the base.\n"
        "2. Check each option to see if it contains any invalid digits.\n"
        "3. The correct answer will have a digit that is not allowed in the specified base."
    )
    return question, options, correct_answer, explanation