English subtitles

← 06-23 Row Plays

Get Embed Code
1 Language

Showing Revision 1 created 06/29/2012 by Amara Bot.

  1. There's our whole row, and while I'm at it I might as well define a hand.
  2. Now my next target, the next bird to cross off the list is to define a function row_plays,
  3. which takes a hand and a row in this format and return a set of legal plays from the row.
  4. Now, rather than just return legal words, I'm using this notion of a play,
  5. where a play is a pair of location within the row and the word that we want to play.
  6. You can imagine it's going to take the same general approach that we've used before,
  7. start with an empty set, do something to it, and then return the results that we built up.
  8. What is it that we want to do? We want to consider each possible allowable prefix,
  9. and to that we want to add all the suffixes, keeping the words.
  10. Now, prefixes of what? That's the first thing to figure out.
  11. What I'm going to do is enumerate the row--enumerate actually just the good bits.
  12. The row from the first position to the last position, and that tells me I don't want the borders.
  13. I don't want to consider playing on the borders.
  14. I just want to consider playing on the interior of the row.
  15. Enumerate that starting from position number 1.
  16. One would be where the A is.
  17. Now I have an index--a number 1, 2, 3--and I have the square, which is going to be a,
  18. and then an anchor and then an anchor and so on.
  19. Where do I want to consider my rows?
  20. We're going to anchor them on an anchor so I can ask a square an instance of an anchor.
  21. If it is an anchor, then there's two possibilities.
  22. If it's an anchor like this, there's only one allowable prefix.
  23. The prefix which is the letters that are already there just to the left of the anchor.
  24. We want to just consider that one prefix and then add all the suffixes.
  25. If it's an anchor like this one, then there can be many prefixes.
  26. We want all possible prefixes that fit into these spots here,
  27. consider each one of those, and for each one of those consider adding on the suffixes.
  28. What I'm going to do is define a function, legal _prefix,
  29. which gives me a description of the legal prefix that can occur at position i within a row.
  30. There are two possibilities.
  31. I could combine the possibilities into one, but I'm going to have a tuple of two values returned.
  32. I'm going to have legal_prefix return the actual prefix as a string
  33. if there is one, like in this case, and return the maximum size otherwise.
  34. For this anchor here, this would be legal_prefix of one, two, three, four, five, six--
  35. that's for legal_prefix when i = 6.
  36. The result would be that there are now characters to the left.
  37. It'll be the empty string for the first element of the tuples.
  38. The maximum size of the prefix that I'm going to allow is two characters.
  39. Now, if I asked here--that's index number one, two, three, four, five, six, seven, eight, nine--
  40. when i = 9, the result would be that the prefix is BE,
  41. and the maximum size is the same as the minimum size. It's the exact size of 2.
  42. I define legal_prefix in order to tell me what to do next based on the two types of anchors.
  43. Now, I can go back to row plays.
  44. I can call legal_prefix, get my results, and say if there is a prefix,
  45. then I want to add to the letters already on the board.
  46. Otherwise, I have an empty space to the left, and I want to go through all possible prefixes.
  47. Here's what we do if there is a prefix already there.
  48. Now we can calculate the start of our position.
  49. Remember a row play is going to return the starting location of the word.
  50. We can figure that out. It's the i position of the anchor minus the length of the prefix.
  51. In fact, let me go and change this comment here.
  52. I is not very descriptive.
  53. Let's just call that start.
  54. Now we know what the starting location is for the word.
  55. When we find any words we can return that.
  56. Then we go ahead and add suffixes.
  57. With the suffixes, some of the letters are going to come out of the hand.
  58. We're adding suffixes to the prefix that's already there on the board.
  59. Starting in the start location, going through the row,
  60. accumulating the results into the result set,
  61. and then I needed this one more argument.
  62. I actually made a mistake and left this out the first time, and it didn't work.
  63. We'll see in a bit what that's there for.
  64. Now if we have empty space to the left of the anchor,
  65. now we've got to go through all the possible prefixes,
  66. but we already wrote that function--find_prefixes. That's good.
  67. Looks like we're converging. We're not writing that much new stuff.
  68. Now, out of all the possible prefixes for the hand, we only want to look at the ones
  69. that are less than or equal to the maximum size.
  70. If the prefix is too big, it won't fit into the empty spot.
  71. It will run into another word, and we don't want to allow that.
  72. We can calculate the start position again.
  73. Then we do the same thing. We add suffixes. What do we add them to?
  74. We'll the prefix that we just found from the hand.
  75. Since the prefix came from the hand, the remaining letters left in the hand
  76. we have to subtract out those prefix letters.
  77. Here we didn't have to subtract them out, because they prefix letters were already on the board.
  78. We're adding to the prefix from the start, from the row, results are accumulated,
  79. and we have this anchored equals false again.
  80. We're almost there. Just two things left to do--add_suffixes and legal_prefix.
  81. Add_suffixes we had before, but it's going to be a little bit more complicated now,
  82. because we're dealing with the anchors.
  83. Legal_prefix is just a matter of looking to the left and see how much space is there.