Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # abc2num.py
- import string
- #
- def abc2num(z):
- num = z.replace('-','')
- digits = string.ascii_uppercase
- r = 0
- for exp, char in enumerate(reversed(num)):
- r = r + (pow(26, exp) * (digits.index(char) + 1))
- return r
- #
- def num2abc(div):
- abc=''
- while div:
- (div, mod) = divmod(div-1, 26)
- abc = chr(mod + 65) + abc
- return abc
- #
- abc='ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- print abc2num('A') # => 1
- print abc2num('Z') # => 26
- print abc2num('AA') # => 27
- print abc2num('CZ') # => 104
- print abc2num('ZZZ') # => 18278
- print abc2num('Z'*21) # => 538857146126462423479278937046
- print abc2num(abc) # => 256094574536617744129141650397448476
- print num2abc(1) # => A
- print num2abc(26) # => Z
- print num2abc(27) # => AA
- print num2abc(104) # => CZ
- print num2abc(26**3+26**2+26) # => ZZZ
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement