fork download
  1. def something_about_bases(number, bonus=False):
  2. assert isinstance(number, str)
  3.  
  4. a = -ord('a') + 10
  5. nums = [ord(n) + a if n.isalpha() else int(n) for n in number]
  6.  
  7. smallest_base = max(nums) + 1
  8. top_base = 16 if bonus else smallest_base
  9.  
  10. for base in range(smallest_base, top_base + 1):
  11. val = sum(n * base ** (len(nums) - i - 1) for i, n in enumerate(nums))
  12. print("base %i => %i" % (base, val))
  13.  
  14. inputs = ['1', '21', 'ab3', 'ff']
  15. for i in inputs:
  16. something_about_bases(i)
  17.  
  18. # bonus 1
  19. print('')
  20. something_about_bases('21', bonus=True)
  21.  
  22. # bonus 2
  23. print('')
  24. something_about_bases('0')
Success #stdin #stdout 0s 9024KB
stdin
Standard input is empty
stdout
base 2 => 1
base 3 => 7
base 12 => 1575
base 16 => 255

base 3 => 7
base 4 => 9
base 5 => 11
base 6 => 13
base 7 => 15
base 8 => 17
base 9 => 19
base 10 => 21
base 11 => 23
base 12 => 25
base 13 => 27
base 14 => 29
base 15 => 31
base 16 => 33

base 1 => 0