Skip to content

Instantly share code, notes, and snippets.

@PsychedelicShayna
Last active November 13, 2023 16:06
Show Gist options
  • Save PsychedelicShayna/887b8524f95a6a51be891f9f7b5cf5cf to your computer and use it in GitHub Desktop.
Save PsychedelicShayna/887b8524f95a6a51be891f9f7b5cf5cf to your computer and use it in GitHub Desktop.
This is a sort of rudimentary concise language-agnostic esoteric word hashing algorithm that's easy to copy+paste into existing code, not designed to obfuscate the word, and with no regards to collision prevention.
'''
This is a sort of rudimentary concise language-agnostic esoteric word hashing algorithm that's easy to copy+paste
into existing code, not designed to obfuscate the word, and with no regards to collision prevention.
Condenses words down to 4 characters, pads words less than 4 characters, retains its ability to be identified
as a same-length string, with knowledge of the original set, and that the machine can also use, for example,
to compute an offset in a contiguous array, among other things.
'''
def str_abbr(input: str, limit: int = 4, pad: str = '.') -> str | None:
if not isinstance(input, str) and isinstance(limit, int) and isinstance(pad, str):
return None
input_length: int = len(input)
if limit < 4:
return input
elif input_length == limit:
return input
elif input_length < limit:
return input + (pad * (limit - input_length))
# Return value
abbreviation: str = ""
arrangement: str = "{first}{left}{right}{last}"
input_even: bool = input_length % 2 == 0
if input_even:
left_half: str = input[:(input_length // 2) - 1]
right_half: str = input[(input_length // 2) + 1:]
halves_even: bool = (len(left_half) % 2 == 0) and (len(right_half) % 2 == 0)
if halves_even:
return arrangement.format(
first = input[0],
left = left_half[len(left_half) // 2],
right = right_half[(len(right_half) // 2) - 1],
last = input[-1]
)
else:
return arrangement.format(
first = input[0],
left = left_half[len(left_half) // 2],
right = right_half[len(right_half) // 2],
last = input[-1]
)
else:
left_half: str = input[:input_length // 2]
right_half: str = input[(input_length // 2) + 1:]
halves_even: bool = (len(left_half) % 2 == 0) and (len(right_half) % 2 == 0)
if halves_even:
return arrangement.format(
first = input[0],
left = left_half[len(left_half) // 2],
right = right_half[(len(right_half) // 2) - 1 ],
last = input[-1]
)
else:
return arrangement.format(
first = input[0],
left = left_half[len(left_half) // 2],
right = right_half[len(right_half) // 2],
last = input[-1]
)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment