Created
August 1, 2018 13:53
-
-
Save ievans3024/158c2e2d5a47a5d36077a3f01ff00aa2 to your computer and use it in GitHub Desktop.
An overengineered way to get what 1-based index a letter is in the roman alphabet
This file contains hidden or 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
| import struct | |
| """ | |
| An overengineered way to get what 1-based index a letter is in the roman alphabet | |
| """ | |
| class AlphaBytes: | |
| UPPERCASE = set([struct.pack('>b', i) for i in range(65, 91)]) | |
| LOWERCASE = set([struct.pack('>b', i) for i in range(97, 123)]) | |
| def alphabet_index(character): | |
| cbytes = bytes(character, 'ascii') | |
| if cbytes in AlphaBytes.UPPERCASE: | |
| modifier = -64 | |
| elif cbytes in AlphaBytes.LOWERCASE: | |
| modifier = -96 | |
| return struct.unpack('>b', cbytes)[0] + modifier |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment