Created
August 24, 2019 19:41
-
-
Save MineRobber9000/0f6bd36a15846d622a64edebfbf9b177 to your computer and use it in GitHub Desktop.
Solve a word scramble (such as "what word is made from the letters 'ysujfit'?")
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
#!/usr/bin/python3 | |
import argparse | |
# This is the list of words that will be used to search for the word. | |
# Format is one word per line. | |
# The script will ignore lines starting with a hash. | |
WORDLIST = "/usr/share/dict/words" # default word list on most Linux boxen | |
def str_sorted(word): | |
"""Returns a string with sorted characters.""" | |
return "".join(sorted(word)) # sorted(str) gives a sorted list of each character, so joining them back together is easy. | |
with open(WORDLIST) as f: words = [l.strip() for l in f if l.strip() and not l.startswith("#")] # eliminate empty lines in word list and comments | |
words_sortedletters = [str_sorted(x) for x in words] | |
def find(jumble): | |
"""Finds the word represented by `jumble`.""" | |
jumble = str_sorted(jumble) | |
try: | |
i = words_sortedletters.index(jumble) | |
return words[i] | |
except ValueError: # item not in list | |
return None | |
if __name__=="__main__": | |
parser = argparse.ArgumentParser(description="Unscrambles words.") | |
parser.add_argument("word",help="Word to unscramble.") | |
args = parser.parse_args() | |
w = find(args.word) | |
if w is None: | |
print("Word not found! Maybe try another wordlist?") | |
else: | |
print("The word you are looking for is {}.".format(w)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment