Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Number Base by Rafal__Kotas
def checkio(str_number, radix):
n = str(str_number)
r=int(radix)
liczba = 0
p = True
for j in range(len(n)):
if(n[j]=='0' or n[j]=='1' or n[j]=='2' or n[j]=='3' or n[j]=='4' or n[j]=='5' or n[j]=='6' or n[j]=='7' or n[j]=='8' or n[j]=='9'):
if(radix == int(n[j]) or radix < int(n[j])):
p = False
else:
if(radix == (ord(n[j])-55) or radix < (ord(n[j])-55)):
p = False
for i in range(len(n)):
if(n[i]=='0' or n[i]=='1' or n[i]=='2' or n[i]=='3' or n[i]=='4' or n[i]=='5' or n[i]=='6' or n[i]=='7' or n[i]=='8' or n[i]=='9'):
liczba = liczba + int(n[i])*(radix**(len(n)-1-i))
else:
liczba = liczba + (ord(n[i])-55)*(radix**(len(n)-1-i))
if(p == True):
return liczba
else:
return -1
#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. 1, 2016