Last active
July 15, 2022 08:48
-
-
Save bennuttall/c149f85251449053c7bfb1a4d08b37a7 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
from string import ascii_lowercase | |
import argparse | |
import random | |
LINUX_SYSTEM_DICT = '/usr/share/dict/american-english' | |
MAC_SYSTEM_DICT = '/usr/share/dict/words' | |
BIG_WORDLIST = 'words.txt' | |
# wordlist = LINUX_SYSTEM_DICT | |
wordlist = MAC_SYSTEM_DICT | |
# wordlist = BIG_WORDLIST | |
with open(wordlist) as f: | |
WORDS = { | |
word.lower().strip() | |
for word in f | |
if all(c.lower() in ascii_lowercase for c in word.strip()) | |
} | |
def encode(word: str) -> str: | |
n = len(word) - 2 | |
return f"{word[0]}{n}{word[-1]}" | |
def decode(numeronym: str) -> str: | |
a, *b, c = numeronym | |
b = int(''.join(b)) | |
return { | |
word | |
for word in WORDS | |
if len(word) == b + 2 | |
and word[0] == a | |
and word[-1] == c | |
} | |
def main(args): | |
if args.encode is not None: | |
print(encode(args.encode)) | |
if args.decode is not None: | |
words = decode(args.decode) | |
if len(words) == 0: | |
print("No words found") | |
if args.lucky: | |
print(random.choice(list(words))) | |
else: | |
for word in words: | |
print(word) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-e', '--encode', type=str) | |
parser.add_argument('-d', '--decode', type=str) | |
parser.add_argument('-l', '--lucky', action='store_true') | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good work!