Skip to content

Instantly share code, notes, and snippets.

@simonLeary42
Last active December 19, 2024 19:09
Show Gist options
  • Save simonLeary42/34a2ec8c555d1de5e65ff0a9b5d2b15a to your computer and use it in GitHub Desktop.
Save simonLeary42/34a2ec8c555d1de5e65ff0a9b5d2b15a to your computer and use it in GitHub Desktop.
def _make_string_sortable_numerically(string:str) -> List[Tuple[int, int]]:
"""
each character becomes a tuple of two ints. The first int is either 0,1, or 2
0 for characters that come before numbers, 1 for numbers, 2 for after numbers
the second int is the unicode value of the character, or the integer value of the number
that this character is a part of.
$ 7 8 9 a ~
"$789a~" -> [[0, 36], [1, 789], [1, 789], [1, 789], [2, 97], [2, 126]]
"""
output = [[None, None] for _ in range(len(string))]
skip_these_indexes = [False]*len(string)
for i, char in enumerate(string):
if skip_these_indexes[i]:
continue
char_int = ord(char)
if char_int < ord("0"):
output[i] = (0, char_int)
elif str.isdigit(char):
first_digit_index = i
last_digit_index = i
while (last_digit_index < len(string)-1 and str.isdigit(string[last_digit_index+1])):
last_digit_index += 1
this_number = int(string[first_digit_index:last_digit_index+1])
for digit_index in range(first_digit_index, last_digit_index+1):
skip_these_indexes[digit_index] = True
output[digit_index] = (1, this_number)
elif char_int > ord("9"):
output[i] = (2, char_int)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment