Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions