Problem Set 6; Problem 3 of 5, Part 1
The first function it asks you to create is one called pick_best_hand(). This function is to take in the hand and points_dict variables and return the highest scoring word from points_dict that can be made with the given hand. If no words can be made with the given hand, then it is to return ‘.’. This is the end function I was to create, however there were several different steps I had to write before pick_best_hand() could be made. One of the first steps to doing so was to create a helping function called get_words_to_points(). It is shown below.
def get_words_to_points(word_list): points_dict = {} for word in word_list: points_dict[word] = get_word_score(word,n) return points_dict points_dict = get_words_to_points(word_list) |
This function was to take in the word_list (a list of all possible words to be created, based on a scrabble dictionary), and produce a dictionary that would have the word as the key and it’s total points as the value attached to it. Thereby creating a dictionary to use, when determining the highest scoring word possible.
To begin with I needed to define a new dictionary within my function, that would be returned once filled and completed. I named this points_dict = {}. Then I simply used a for loop and went through each word in the word_list. For each word I simply ran that word against a former function I created, the get_word_score(word,n) function. This function simply takes any provided word and spits out it’s point value.
To make the code more easy to read I went ahead and assigned each word I processed and it’s value (get_word_score(word,n)) to the dictionary as the for loop continued to process information. Once the word_list was entirely processed, all point values would have been attached to their appropriate word, and the new dictionary point_dict would be finished. The only additional step this part asked, was to assign the end value of the get_words_to_points() function to a variable that could later be called upon. This variable name was points_dict.
Difficulties? Not really, as this was a basic for loop problem with the addition of creating a dictionary along the way. Learning how to write this code simplistically will make future such problems much easier to read.
Problem Set 5 - a correction to a previously made function called update_hand()