Created
July 21, 2019 04:16
-
-
Save gutierrezps/a7206080a83e26fd4f69821294cf4d45 to your computer and use it in GitHub Desktop.
Converts a list index into letter(s)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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