Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Number Base by pangas
def checkio(str_number: str, radix: int) -> int:
answer = 0
letters = {"0":0,"1":1,"2":2,"3":3,"4":4,"5":5,"6":6,"7":7,"8":8,"9":9,
"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}
power = len(str_number)-1
for x in str_number:
if letters[x] >= radix:
answer = -1
break
answer += letters[x]*(radix**power)
power -= 1
return answer
#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!")
Sept. 29, 2018