Advertisement
here2share

# abc2num.py

Mar 3rd, 2017
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.80 KB | None | 0 0
  1. # abc2num.py
  2.  
  3. import string
  4. #
  5. def abc2num(z):
  6.     num = z.replace('-','')
  7.     digits = string.ascii_uppercase
  8.     r = 0
  9.     for exp, char in enumerate(reversed(num)):
  10.         r = r + (pow(26, exp) * (digits.index(char) + 1))
  11.     return r
  12. #
  13. def num2abc(div):
  14.     abc=''
  15.     while div:
  16.         (div, mod) = divmod(div-1, 26)
  17.         abc = chr(mod + 65) + abc
  18.     return abc
  19. #
  20.  
  21. abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  22.  
  23. print abc2num('A')      # => 1
  24. print abc2num('Z')      # => 26
  25. print abc2num('AA')     # => 27
  26. print abc2num('CZ')     # => 104
  27. print abc2num('ZZZ')    # => 18278
  28. print abc2num('Z'*21)   # => 538857146126462423479278937046
  29. print abc2num(abc)      # => 256094574536617744129141650397448476
  30.  
  31. print num2abc(1)                # => A
  32. print num2abc(26)               # => Z
  33. print num2abc(27)               # => AA
  34. print num2abc(104)              # => CZ
  35. print num2abc(26**3+26**2+26)   # => ZZZ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement