Skip to content

Instantly share code, notes, and snippets.

@simrit1
Forked from Neeharika7/scrabble.py
Created March 14, 2022 18:01
Show Gist options
  • Select an option

  • Save simrit1/2b105116a9c257df34b3811e4f025f2a to your computer and use it in GitHub Desktop.

Select an option

Save simrit1/2b105116a9c257df34b3811e4f025f2a to your computer and use it in GitHub Desktop.
scrabble game developed using python
def count_letters(word):
count = {}
for letter in word:
if letter not in count: count[letter] = 0
count[letter] += 1
return count
def spellable(word, rack):
word_count = count_letters(word)
rack_count = count_letters(rack)
return all( [word_count[letter] <= rack_count[letter] for letter in word] )
score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
"f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
"l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
"r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
"x": 8, "z": 10}
def score_word(word):
return sum([score[c] for c in word])
def word_reader(filename):
# returns an iterator
return (word.strip() for word in open(filename))
if __name__ == "__main__":
import sys
if len(sys.argv) == 2:
rack = sys.argv[1].strip()
else:
print("Usage: python cheat_at_scrabble.py <yourrack>")
exit()
words = word_reader('/usr/share/dict/words')
scored = ((score_word(word), word) for word in words if set(word).issubset(set(rack)) and len(word) > 1 and spellable(word, rack))
for score, word in sorted(scored):
print (str(score), '\t', word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment