Problem Set 6; Problem 3 of 5, Part 2
Suffice it to say, I didn’t do this at all. I still ran is_valid_word() separately and used the points_dict variable to handle any point creation. Because I did not combine the two, it made some of my later adjustments, perhaps, more complicated. My apologies in advance for any ridiculous looking code.
The next function I created is a generator for the computer player, that will create combinations of letters based on the hand. To do this I implemented a brute force algorithm. Typically brute force algorithms are very poor choices, unless the information you’re processing is minimal. My function is shown below, it’s recursive.
def comb(chosen, available): finallist = [chosen,] for a in available: newavail = available[:] newavail.remove(a) finallist += comb(chosen + [a,], newavail) return finallist |
This function creates a new list (of lists) called finallist, that returns all possible combinations from the letters provided in the hand. It starts off by going through each letter in available (which is the same as hand). For each letter it creates a new combination of letters by taking a slice, removing the letter being processed, then adding a new list to the finallist by recursively calling itself. Once completed this returns a list of lists, that might look something like this: ((‘a’,’b’,’c’), (‘b’,’c’), (‘a’,’c’), (‘a’)). This example is not fully complete, because it does not show all possible combinations or different ways in which the letters could be organized.
Up Next Time: Problem Set 6; Problem 3 of 5, Part 3