File size: 2,388 Bytes
31d5315
c9ca893
58956eb
 
d924141
58956eb
 
c9ca893
58956eb
 
 
 
31d5315
 
 
 
 
58956eb
 
31d5315
 
 
58956eb
31d5315
 
 
 
 
 
 
 
 
 
 
3a56900
58956eb
 
3a56900
58956eb
 
 
 
 
 
 
 
 
31d5315
58956eb
 
 
 
 
c9ca893
58956eb
 
 
 
 
 
 
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
59
60
61
62
# modules/number_system/valid_invalid_numbers.py

title = "Validity of Numbers in Bases"
description = "This module helps determine the validity of numbers in different bases like binary, octal, decimal, and hexadecimal."

def generate_question():
    import random
    
    base = random.choice([2, 8, 10, 16])
    length = random.randint(3, 5)
    
    def generate_valid_number():
        if base == 16:
            valid_digits = '0123456789ABCDEF'
        else:
            valid_digits = ''.join([str(i) for i in range(base)])
        return ''.join([random.choice(valid_digits) for _ in range(length)])
    
    def generate_invalid_number():
        if base == 16:
            valid_digits = '0123456789ABCDEF'
            invalid_digit = random.choice('GHIJKLMNOPQRSTUVWXYZ')
        else:
            valid_digits = ''.join([str(i) for i in range(base)])
            if base < 10:
                invalid_digit = str(random.randint(base, 9))  # For bases < 10, pick an invalid digit from [base, 9]
            else:
                invalid_digit = random.choice('GHIJKLMNOPQRSTUVWXYZ')
        
        # Replace a random digit in a valid number with an invalid digit
        valid_number = list(generate_valid_number())
        replace_index = random.randint(0, length - 1)
        valid_number[replace_index] = invalid_digit
        return ''.join(valid_number)

    correct_answer = generate_invalid_number()
    options = [correct_answer]

    # Generate valid options
    while len(options) < 4:
        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 allowed for base {base}."
    step_by_step_solution = [
        "Step 1: Identify the valid digits for the base.",
        "Step 2: Check each option to see if it contains any invalid digits.",
        "Step 3: The correct answer will have a digit that is not allowed in the specified base."
    ]
    
    return {
        "question": question,
        "options": options,
        "correct_answer": correct_answer,
        "explanation": explanation,
        "step_by_step_solution": step_by_step_solution
    }