This problem came to me more easily, however the end solution is not quite my own.  I learned how to use dict.get, but it hasn’t truly sunk in yet.  The premise of this problem is simply to verify that the word submitted is indeed an approved and real word and then to make sure that the player has submitted a word that be created out of the letters that appear in their hand.  Sounds simple, right?  Well it is, mostly.  We already have, in this Problem Set, the provided word_list which gives us something to compare all submitted words, to verify for authenticity.  Here is the official problem description.

This function should return True if the word is in the word_list and is entirely composed of letters in the hand. Otherwise, it returns False.  This function should not mutate the hand or word_list.

Like I mentioned above, this problem is fairly simply.  The implementation is fairly simple, there was only one real hump I had to get over.  One that I’m still working on.  Understanding how get() works.  Get() is a function one can use on dictionaries (a method of dictionaries, if you want to get technical).  As described on, it; returns the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.  To better explain, here is an example dictionary:
hand = {'a':1, 'q':1, 'l':2, 'm':1, 'u':1, 'i':1}
Hand is the name of the dictionary.  A key is the letters, such as ‘a’, or ‘m’.  The value is the frequency in which the letters occur in the hand, or the number listed next to each letter (key).  To use get(), one could simply type print hand.get(‘a’,0), and you would get the number 1 printed in the next line.  If the letter you were looking for did not exist in the dictionary it would return ‘0’.  Learning how to use get() was very useful, as it simplified my function greatly.

def is_valid_word(word, hand, word_list):
   newlist = get_frequency_dict(word)
   newhand = hand.copy()
   if word in word_list:
       for letter in newlist:
           if newhand.get(letter,0) < newlist[letter]:
                return False
       return True            
       return False
First I create a new dictionary based off of the word submitted, and call it newlist.  Then I copy the hand that was submitted to the function, so that I don’t alter it during my processing.  Then I verify that the word submitted is in the word_list, by using an if statement.  Afterwards I run a for loop for each letter in the newlist, and verify that there are enough appropriate letters needed from the newhand to make up the word.  If all is True (correct), the function will simply return True, and the code is complete.  

Up Next: More Python Love