forked from cbasurto/MIT-Edx-Problem-Sets-Intro-to-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem Set 4 Valid Words
More file actions
33 lines (29 loc) · 1.16 KB
/
Copy pathProblem Set 4 Valid Words
File metadata and controls
33 lines (29 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#At this point, we have written code to generate a random
#hand and display that hand to the user.
#We can also ask the user for a word (Python's raw_input) and score the word
#(using your getWordScore). However, at this point we have not written any
#code to verify that a word given by a player obeys the rules of the game.
#A valid word is in the word list; and it is composed entirely of letters
#from the current hand. Implement the isValidWord function.
def isValidWord(word, hand, wordList):
"""
Returns True if word is in the wordList and is entirely
composed of letters in the hand. Otherwise, returns False.
Does not mutate hand or wordList.
word: string
hand: dictionary (string -> int)
wordList: list of lowercase strings
"""
new_hand = hand.copy()
if word in wordList:
for letter in word:
try:
if (letter not in new_hand) or (new_hand.get(letter) == 0):
return False
else:
new_hand[letter] = new_hand.get(letter) - 1
except KeyError:
return False
return True
else:
return False