Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Number Base by kamilinho20
def checkio(str_number, radix):
lengt = len(str_number) - 1
iterator = lengt
result = 0
while iterator >= 0:
a = 0
if ord(str_number[iterator]) >= 65 and ord(str_number[iterator]) <= 90:
a = ord(str_number[iterator]) - 55
else:
a = int(str_number[iterator])
if a > (radix - 1):
return -1
result += (radix**(lengt - iterator) * a)
iterator -= 1
print(result)
return result
#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"
Dec. 14, 2016