Skip to main content

Command Palette

Search for a command to run...

Base Check

Published
4 min readView as Markdown
Base Check

The challenge addressed here is about determining whether the given number is in the correct base. Let’s first examine the problem.

Actual Problem

Given a string representing a number, and an integer base from 2 to 36, determine whether the number is valid in that base.

  • The string may contain integers and uppercase or lowercase characters.

  • The check should be case-insensitive.

  • The base can be any number 2–36.

  • A number is valid if every character is a valid digit in the given base.

  • Example of valid digits for bases:

    • Base 2: 0–1

    • Base 8: 0–7

    • Base 10: 0–9

    • Base 16: 0–9 and A–F

    • Base 36: 0–9 and A–Z


When you read this problem, most of you may think

Oh I could code it like,

baseTwo = "01"
baseEight = "01234567"
baseTen = "0123456789"
baseSixteen = "0123456789ABCDEF"
basaeThirtySix = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

But if you look at the Tests that our code needed to pass, they are like below

  • 1. is_valid_number("10101", 2) should return True.

  • 2. is_valid_number("10201", 2) should return False.

  • 3. is_valid_number("76543210", 8) should return True.

  • 4. is_valid_number("9876543210", 8) should return False.

  • 5. is_valid_number("9876543210", 10) should return True.

  • 6. is_valid_number("ABC", 10) should return False.

  • 7. is_valid_number("ABC", 16) should return True.

  • 8. is_valid_number("Z", 36) should return True.

  • 9. is_valid_number("ABC", 20) should return True.

  • 10. is_valid_number("4B4BA9", 16) should return True.

  • 11. is_valid_number("5G3F8F", 16) should return False.

  • 12. is_valid_number("5G3F8F", 17) should return True.

  • 13. is_valid_number("abc", 10) should return False.

  • 14. is_valid_number("abc", 16) should return True.

  • 15. is_valid_number("AbC", 16) should return True.

  • 16. is_valid_number("z", 36) should return True.

Then you can see that our code needs to check the validation dynamically, whether the number is in the correct base.

There may be several methods to solve this challenge. But I will explain the solution that I came up with. And later in this article, I will explain what is happening in the code.

Let’s look at the solution

def is_valid_number(n, base):
    if not (2 <= base <= 36):
        return False

    digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    valid_chars = digits[:base]
    n = n.upper()

    return all(char in valid_chars for char in n)

Explanation

if not (2 <= base <= 36):
    return False

Our solution needs to check the base validation from base 2 to base 36. That means if the “base” is below 2 or if the “base” is above 36, then it should return False. So that validation is happening in these two lines of code.

digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
valid_chars = digits[:base]
n = n.upper()

Since we need to validate dynamically, what we could do is define every possible character in a one variable. That’s done in the first line of code. Since we have “base 36” maximum characters will be from 0 to “Z”.

Then we are taking the valid characters for each base dynamically from the above-defined variable.

Let’s say our base is 2, then the valid characters should be 0 and 1. Let’s see how it’s coming dynamically from the code,

  • valid_chars = digits[:2]

  • This will pick the characters from the 0th index to the 2nd index, which are 0 and 1

After getting the valid characters, we need to make sure that the validation is case-insensitive. For that, we can make the “n” string uppercase before validation. That’s done in the third line of code.

return all(char in valid_chars for char in n)

In this line of code, “char in valid_chars for char in n“, this is called a generator function, which is used for validation. It is checking whether each and every character in the “n” is in the “valid_chars”. And then “all()” function returns “True” if all the characters of “n” are in the “valid_chars”, otherwise “False”.

Example

Let’s take

is_valid_number("ABC", 20) should return True.

this scenario for an example.

This means our “n” will be “ABC” and the “base” will be 20.

So our “valid_chars” will be “0123456789ABCDEFGHIJ”. Then we are making “n” to be uppercase, but our “n” is already in uppercase.

Then it checks whether the characters of “n” are in the “valid_chars”.

Are all the characters of “n” actually in the “valid_chars” string? Yes, It is. Then our function returns “True”.

I believe you got an idea about how to solve these types of problems. There could be many ways of solving this, but I explained only one of them.

Summary

This article explores how to determine if a given number is valid in a specified base, ranging from 2 to 36. The solution involves dynamically verifying character validity against the base using a predefined set of permissible digits and letters, and ensuring case insensitivity. By utilizing a combination of slicing and a generator function, each digit in the number is checked against valid characters for the base, ensuring accurate validation.