Base Check

Run Settings
LanguagePython
Language Version
Run Command
def is_valid_number(n, base): baseTwo = {chr(i) for i in range(ord('0'), ord('1') + 1)} baseEight = {chr(i) for i in range(ord('0'), ord('7') + 1)} baseTen = {chr(i) for i in range(ord('0'), ord('9') + 1)} baseSixteen = {chr(i) for i in range(ord('0'), ord('9') + 1)} | {chr(i) for i in range(ord('A'), ord('F') + 1)} baseThirtyTwo = {chr(i) for i in range(ord('0'), ord('9') + 1)} | {chr(i) for i in range(ord('A'), ord('Z') + 1)} nUpperCase = n.upper() if base >= 2 and base < 8: return all(char in baseTwo for char in nUpperCase) if base >= 8 and base < 10: return all(char in baseEight for char in nUpperCase) if base >= 10 and base < 16: return all(char in baseTen for char in nUpperCase) if base >= 16 and base < 32: return all(char in baseSixteen for char in nUpperCase) if base >= 32: return all(char in baseThirtyTwo for char in nUpperCase) return False """ 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 """ result = is_valid_number("10101", 2) print(f"10101 is valid number for base 2? {result}") result = is_valid_number("9876543210", 8) print(f"9876543210 is valid number for base 8? {result}")
Editor Settings
Theme
Key bindings
Full width
Lines