jprichter

ps5

Oct 30th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.62 KB | None | 0 0
  1. def is_valid_word(word, hand, word_list):
  2.     """
  3.    Returns True if word is in the word_list and is entirely
  4.    composed of letters in the hand. Otherwise, returns False.
  5.    Does not mutate hand or word_list.
  6.    
  7.    word: string
  8.    hand: dictionary (string -> int)
  9.    word_list: list of lowercase strings
  10.    """
  11.     # TO DO ...
  12.     new_hand = {}
  13.     new_hand.update(hand)
  14.  
  15.     if word in word_list:
  16.         for letter in word:
  17.             new_hand[letter] = new_hand.get(letter,0) - 1
  18.             if new_hand.get(letter,0) < 0 :
  19.                 return False
  20.         return True
  21.     else:
  22.         return False
  23.  
Advertisement
Add Comment
Please, Sign In to add comment