Created
February 18, 2019 01:52
-
-
Save paulhendricks/c3ab98dcb32a934d8b9fbaca9dd73022 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
""" | |
Iterates through all possible word combinations and checks if they are in the | |
English dictionary; if so, calculates the value for that play. Finally, prints | |
all solutions in descending order of value. | |
Setup: pip install pyenchant | |
Usage: python words_with_friends.py --letters "a,r,f,i,b,a,o" --values "1,1,4,4,1,4,1,1" | |
""" | |
import argparse | |
import enchant | |
import itertools | |
import operator | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--letters', metavar='N', type=str, help='comma separated string of letters') | |
parser.add_argument('--values', metavar='N', type=str, help='comma separated string of values') | |
args = parser.parse_args() | |
letters = args.letters.split(',') | |
values = args.values.split(',') | |
mapping = {} | |
for l, v in zip(letters, values): | |
if l not in mapping: | |
mapping[l] = int(v) | |
# print(mapping) | |
d = enchant.Dict("en_US") | |
solutions = {} | |
n = len(letters) | |
for i in range(n-1, 0, -1): | |
for subset in itertools.combinations(letters, i): | |
word = ''.join(subset) | |
if d.check(word): | |
value = 0 | |
for letter in word: | |
value += mapping[letter] | |
solutions[word] = value | |
# print(word, ':', value) | |
# sort using highest | |
solutions = sorted(solutions.items(), key=operator.itemgetter(1), reverse=True) | |
# print solutions with highest value solutions first | |
for word, value in solutions: | |
print(word, ':', value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment