Skip to content

Instantly share code, notes, and snippets.

@saltavenger
Created October 31, 2012 00:49
Show Gist options
  • Save saltavenger/3984129 to your computer and use it in GitHub Desktop.
Save saltavenger/3984129 to your computer and use it in GitHub Desktop.
scrabble3
def compChooseWord(hand, wordList):
"""
Given a hand and a wordList, find the word that gives
the maximum value score, and return it.
This word should be calculated by considering all the words
in the wordList.
If no words in the wordList can be made from the hand, return None.
hand: dictionary (string -> int)
wordList: list (string)
returns: string or None
"""
# Create a new variable to store the maximum score seen so far (initially 0)
maxScore = 0
# Create a new variable to store the best word seen so far (initially None)
bestWord = None
# For each word in the wordList
for word in wordList:
# If you can construct the word from your hand
# (hint: you can use isValidWord, or - since you don't really need to test if the word is in the wordList - you can make a similar function that omits that test)
if isValidWord(word, hand, wordList) == True:
# Find out how much making that word is worth
value = getWordScore(word, HAND_SIZE)
#print value
# If the score for that word is higher than your best score
if value > maxScore:
# Update your best score, and best word accordingly
maxScore = value
bestWord = word
#print maxScore
#print bestWord
break
else:
return None
# return the best word you found.
return bestWord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment