Question

Write a Python script that asks the user for their Scrabble rack and prints all valid...

Write a Python script that asks the user for their Scrabble rack and prints all valid Scrabble words that can be constructed from that rack, along with their Scrabble scores, sorted by score. An example input and output: ZAEFIEE

17 feeze 17 feaze 16 faze 15 fiz 15 fez 12 zee 12 zea 11 za 6 fie 6 fee 6 fae 5 if 5 fe 5 fa 5 ef 2 ee 2 ea 2 ai 2 ae

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT