Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Third solution in Clear category for Number Base by colinmcnicholl
import string
def checkio(str_number, radix):
"""Input: Two arguments. A number as string and a radix as an integer.
The function converts the number into decimal form.
The radix is less than 37 and greater than 1.
The task uses digits and the letters A-Z for the strings.
Output: The converted number as an integer.
"""
d = dict(zip(string.digits + string.ascii_uppercase, range(36)))
decimals = [d[num] for num in str_number]
if radix <= max(decimals):
return -1
return sum(dec * radix ** (len(decimals) - idx)
for idx, dec in enumerate(decimals, start=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"
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
May 16, 2019
Comments: