Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Number Base by pawel.sydorow
def checkio(str_number, radix):
num_string = {'A': 10,
'B' : 11,
'C' : 12,
'D' : 13,
'E' : 14,
'F' : 15,
'G' : 16,
'H' : 17,
'I' : 18,
'J' : 19,
'K' : 20,
'L' : 21,
'M' : 22,
'N' : 23,
'O' : 24,
'P' : 25,
'Q' : 26,
'R' : 27,
'S' : 28,
'T' : 29,
'U' : 30,
'V' : 31,
'W' : 32,
'X' : 33,
'Y' : 34,
'Z' : 35,}
listNumber = []
listNumberStr = list(str_number)
for index in range(0, len(listNumberStr)):
try:
value = int(listNumberStr[index])
listNumber.append(value)
if (value >= radix):
return -1
except:
value = num_string[listNumberStr[index]]
listNumber.append(value)
if (value >= radix):
return -1
i = 0
lenght = len(listNumber)
result = 0
while (i < len(listNumber)):
lenght = lenght - 1
result += listNumber[lenght] * radix**i
i = i +1
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"
Nov. 6, 2016