Skip to content

Instantly share code, notes, and snippets.

@gutierrezps
Created July 21, 2019 04:16
Show Gist options
  • Save gutierrezps/a7206080a83e26fd4f69821294cf4d45 to your computer and use it in GitHub Desktop.
Save gutierrezps/a7206080a83e26fd4f69821294cf4d45 to your computer and use it in GitHub Desktop.
Converts a list index into letter(s)
"""
The code below was used to print the letters of each item on
an ordered list. So index 0 becomes 'a', 1 becomes 'b', and
so on.
It also use multiple letters when the list length is greater
than 26, so index 26 becomes 'aa', index 27 becomes 'ab',
and so on.
Author: Gutierrez PS
Date: 2019-07-21
"""
import math
print("Hello World")
def index_to_letters(index):
to_char = lambda i: chr(i % 26 + ord('a'))
remaining = index
out_str = ""
while remaining > 25:
out_str = to_char(remaining) + out_str
remaining = math.floor(remaining / 26) - 1
out_str = to_char(remaining) + out_str
return out_str
for i in range(1,1000):
print(index_to_letters(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment