Question
letter_values = {'a':1, 'b':3, 'c':3, 'd':2, 'e':1, 'f':4, 'g': 2, 'h':4, 'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1, 's':1, 't':1, 'u':1, 'v':8,
letter_values = {'a':1, 'b':3, 'c':3, 'd':2, 'e':1, 'f':4, 'g': 2, 'h':4,
'i':1, 'j':8, 'k':5, 'l':1, 'm':3, 'n':1, 'o':1, 'p':3, 'q':10, 'r':1,
's':1, 't':1, 'u':1, 'v':8, 'w':4, 'x':8, 'y':4, 'z':10}
def scrabbleValue(word):
val=0
for i in word:
val+=letter_values[i]
return val
def bestScrabbleWords(words, howMany=3):
words=sorted(words,key=lambda x:scrabbleValue(x),reverse=True)#reverse sort the value
return words[:howMany]
my_words = ['python', 'program', 'list', 'string', 'unix', 'hours', 'syntax', 'error']
result = bestScrabbleWords(my_words)
print(result)
result = bestScrabbleWords(my_words,5)
print(result)
----------------------------------------------------------------------------------------------------------------------------------
Using thescrabbleValuefunction from top, how I define the functionbestScrabbleTuplesthat takes the list parameterwordsand the optional parameterhowMany, with a default value of 3, and returns a list of the tophowmanytuples where each tuple consists of the (word,value) pair. -- A tuple is just a list with two elements which may be referenced by index 0 and index 1.
def bestScrabbleTuples(words, howMany=3):
return None # TODO: replace this
my_words = ['python', 'program', 'list', 'string', 'unix', 'hours', 'syntax', 'error']
result = bestScrabbleTuples(my_words)
print(result)
result = bestScrabbleTuples(my_words,5)
print(result)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started