Created
May 27, 2022 01:59
-
-
Save demiurg/dce97e353bcb6c1f26e4d9a87b24bac4 to your computer and use it in GitHub Desktop.
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
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
def numToLetter(number): | |
result = ""; | |
charIndex = int(number % len(alphabet)) | |
quotient = int(number / len(alphabet)) | |
if (charIndex - 1 == -1): | |
charIndex = len(alphabet); | |
quotient += 1 | |
result = alphabet[charIndex - 1] + result; | |
if (quotient >= 1): | |
result = numToLetter(quotient) + result; | |
return result | |
def letterToNumber(letters): | |
result = 0; | |
for idx in range(len(letters)): | |
char = letters[idx] | |
number = alphabet.index(char) + 1 | |
result = number + result * idx * len(alphabet) | |
return result | |
if __name__ == "__main__": | |
for x in [1, 27, 100, 200, 600, 649, 650, 700]: | |
print(x, "->", letterToNumber(numToLetter(x))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment