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
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints...
Question1: Write a Python Program that asks for the grades of 5 quizzes and then prints the highest grade. [6 Marks] Question2: Write a Python Program that does the following tasks. [8 Marks] Create an empty list Ask the user for 5 positive numbers and append them to the list Find and print the sum of all the numbers in the list Delete the last element from the list and then print the list Question3: Consider you have the following...
1)Write a program that asks a user for a number. If (and only if) the number...
1)Write a program that asks a user for a number. If (and only if) the number is greater than zero print “The number is valid” 2)Write a program that asks a user for a grade. If (and only if) the grade is greater than zero and less than 101, print “The grade is valid” 3)Write a program that asks a user how many widgets they want to buy and prints out what the user should pay. Widgets costs 10 dollars....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers....
Please use Python 3 4). Write a program that asks the user to enter 10 numbers. The program should store the numbers in a list and then display the following data: • The lowest number in the list • The highest number in the list •The total of the numbers in the list • The average of the numbers in the list   Sample input & output: (Prompt) Enter Number 1: (User enter) 4 (Prompt) Enter Number 2: (User enter) 7...
Write a Python program that reads in the month, day, and year of a date and...
Write a Python program that reads in the month, day, and year of a date and prints it in the dd/mm/yyyy format or mm/dd/yyyy format, the format depending on the user’s preference. The program also prints the message It is a magic date If the product of month and day is equal to the last two digits of the year. For example, April 20 1980 is a magic date because April (numeric value 4) multiplied by 20 is equal to...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT