Python Scrabble Challenge Find Valid Words
I'm trying to complete a python project that basically takes an input and goes through a list of valid scrabble words and determines which of those words can be made given the inpu
Solution 1:
The easiest way to check if words are valid is to use a collections.Counter
. You take the occurrences of each letter in the rack, and the occurrences of each letter for each scrabble word then take the difference. If there's nothing left of the scrabble word after removing the letters from the rack, then you can make the scrabble word.
Example code (use the dictionary provided instead of a system one):
from collections import Counter
withopen('/usr/share/dict/words') as fin:
lines = (word.strip().upper() for word in fin)
words = [(word, Counter(word)) for word in lines]
rack = Counter('AEDTUMS')
for scrabble_word, letter_count in words:
# Using length here to limit output for example purposesiflen(scrabble_word) >= 6andnot (letter_count - rack):
print(scrabble_word)
Will give you:
MEDUSA
AMUSED
SAUTED
Post a Comment for "Python Scrabble Challenge Find Valid Words"