Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Number Base by Bartosz_M
def checkio(str_number, radix):
d = dict()
lenn = len(str_number) - 1
i, sum, counter = 0, 0, 0
last_num = "0"
string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
for letter in string:
d[letter] = i
i += 1
for letter in str_number:
if radix <= d[letter]:
return -1
sum += d[letter] * radix ** (lenn - counter)
counter +=1
return sum
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("AF", 16) == 175, "Hex"
assert checkio("101", 2) == 5, "Bin"
assert checkio("101", 5) == 26, "5 base"
assert checkio("Z", 36) == 35, "Z base"
assert checkio("AB", 10) == -1, "B > A > 10"
Nov. 8, 2016