A unique aspect of this problem set is that the course creators provided a test python file you could run against your file to test the correctness of your functions.  I meant to do that right away when I originally finished the Problem Set, but I didn’t get to it until just a bit ago.  So it seems that I had a mistake in one of my previous entries and I wanted to make sure to update everyone.  It seems that my previous version of the update_hand() function was not entirely accurate.  It almost completed the necessary task, but it missed one thing in particular.  

If you don’t remember, update_hand() is a function that takes the (assumed) correct word submitted, verifies how many letters from the hand that were used to create the word, then deletes them accordingly.  Originally in this function I used a for loop to go through each letter in the hand.  What I didn’t realize at the time was that my original function would only go through each letter once.  Normally this wouldn’t be an issue, unless a word was created using two or more of the same letter.  An example of this would be a hand consisting of the following dictionary of letters and frequencies = {'h': 1, 'e': 1, 'l': 2, 'o': 1}, and the word submitted being ‘hello’.  In this instance my original function would have simply subtracted one from the frequency in which the l occurred, but then moved on to the next letter.  Thus an ‘l’ would have remained in the updated hand, and the function processing would not have correctly solved the problem.

In order to figure out what I was doing wrong I used a common method of debugging, using the print statement.  By having my function print the values of my variables in different places, I can ascertain more acutely where the problem is.  Normally, that is.  Today I was having problems actually getting where I could alter something to fix the issue without having to redo the entire function in order to pass the test file provided.  Here is my corrected function.

 

def update_hand(hand, word):
   newlist = get_frequency_dict(word)
   newhand = hand.copy()
   for letter in newlist:
       if newhand[letter] == newlist[letter]:
           del newhand[letter]
       elif newhand[letter] >= 1:
           newhand[letter] = newhand[letter] - 1
   return newhand

 


It seems what I missed was recognizing that I could simply ask in my for loop if value of the letter in the dictionary newlist was the the same as the letter in the dictionary newhand.  If they were the same, I could simply delete the entire entry from that dictionary.  If it wasn’t, I would simply subtract one from that value, so that the one instance in which that letter showed up was accounted for.  

This alteration does make it so my new function passes the test file, however after some thinking I realized that it would still not be accurate in a couple of rare situations.  Those situations being when three of the same letter or more were present in the hand, and less than that total amount were used in the word submitted.  In such situations I am presented with the same problem I had before.  

So, while my function still works...most of the time...I’m going to show you what another solution to the problem is.  This solution is provided by the course instructors, and presented in the materials for Problem Set 6.  Here is the function they had created.

 

def update_hand(hand, word):
   freq = get_frequency_dict(word)
   newhand = {}
   for char in hand:
       newhand[char] = hand[char]-freq.get(char,0)
   return newhand

 


This function creates two dictionaries, one called freq(based on the results of the get_frequency_dict(word)), and one empty one called newhand.  This function works for all cases mentioned above, because it designates each new key in newhand to be based on the character(letter) being processed in the for loop.  It then designates the value by subtracting the value for that char in the freq dictionary from the value for that particular char in the dictionary hand.  

While this function is fairly simplistic to understand, I know that it didn’t come to mind for me to use a get aspect to solve it because I’m just not that familiar with it.  I understand it, but when solving problems it’s not a tool I think about on hand.  The more I program the more I realize how much it’s utilized.  I think small things like these, small tools that end up being way more helpful than you realize, are going to be the next big thing I need to work on for my programming to get better.

Up Next Time: Problem Set 6, Problem 3 of 5, Part 2