fork download
  1. from functools import reduce
  2.  
  3. def encode(n):
  4. return encode((n-1)//26)+chr((n-1)%26+65) if n>0 else ''
  5.  
  6. def decode(s):
  7. return reduce(lambda x,y:x*26+y-64,map(ord,'\0'+s))
  8.  
  9. def conv(text):
  10. print( '{} => {}'.format( text, encode(int(text)) if text.isdigit() else decode(text)) )
  11.  
  12. conv('A')
  13. conv('AA')
  14. conv('ABC')
  15. conv('26')
  16. conv('266379')
  17.  
Success #stdin #stdout 0.01s 27616KB
stdin
Standard input is empty
stdout
A => 1
AA => 27
ABC => 731
26 => Z
266379 => ODAI