Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for The Good Radix by Rounin
def itoa(num, base):
out = ''
while num > 0:
digit = num % base
out += chr(digit + (ord('0') if digit < 10 else ord('a') - 10))
num //= base
return out[::-1]
def checkio(number):
for radix in range(2, 37):
try:
n = int(number, radix)
if itoa(n, radix).lower() == number.lower() and (n % (radix-1)) == 0:
return radix
except:
None
return 0
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio("18") == 10, "Simple decimal"
assert checkio("1010101011") == 2, "Any number is divisible by 1"
assert checkio("222") == 3, "3rd test"
assert checkio("A23B") == 14, "It's not a hex"
assert checkio("IDDQD") == 0, "k is not exist"
print('Local tests done')
July 29, 2017