Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Creative category for Number Base by jahmee
def checkio(str_number, radix):
lista = list(str_number)
lista.sort()
dl= len(lista)
if str_number.isdigit()==False and (ord(lista[dl-1])-65+10)>radix:
return -1
if str_number.isdigit()==True and int(lista[dl-1])>=radix:
return -1
else:
reversed= str_number[::-1]
suma = 0
for i in range(len(reversed)):
if reversed[i].isdigit()==False:
x=ord(reversed[i])-65+10
suma=suma+(x*radix**i)
else:
suma=suma+(int(reversed[i]) * radix**i)
return suma
#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"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
Oct. 23, 2017
Comments: