Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← 01-51 Summary

Get Embed Code
1 Language

Showing Revision 1 created 04/23/2012 by Amara Bot.

  1. Now let's scroll down and look at the group function.
  2. What group does is it takes a list of items
  3. and then returns a list of counts of items and the item itself.
  4. It just goes through the set of items
  5. and returns pairs of the count
  6. and the set and then sorts them in reverse order
  7. so that the highest comes first.
  8. And now this returns a list of pairs,
  9. and we'd like to turn that into a pair of lists,
  10. and the unzip function does that.
  11. It's a little bit tricky.
  12. You have to look up the zip function and apply it
  13. to the destructured list of the pairs,
  14. but rest assured that that just pulls out,
  15. converts a list of pairs into a pair of lists,
  16. and we call that up here in order to get the right answer.
  17. Now, I'm always thinking about refactoring.
  18. I'm also always thinking of when are you done, and when is it time not to refactor anymore?
  19. But I was having so much fun with this, I just wanted to try one more possibility,
  20. and that's to say rather than have a long case statement
  21. of the different results, can we just have them in a look-up table?
  22. And so what I did is I took all 7 of the partitions of the numbers
  23. of the number 5 and put them into a table called "count_rankings"
  24. and then had the corresponding poker ranking,
  25. 0 for a high card, 1 for a pair, 2 for two pair, and so on.
  26. And now instead of having a statement here that's a long case statement
  27. for all of the 9 possibilities, I can just say
  28. that the result is the maximum of taking the counts,
  29. looking them up in this table, and that will give you a value
  30. for anything having to do with pairs or 3 or 4 of a kind.
  31. And take the maximum of that, and this expression here,
  32. which says 4 times straight plus 5 times flush.
  33. Now, that might seem a little confusing, but what that's doing is
  34. it's using automatic conversion of booleans into integers
  35. and saying if straight is true, that corresponds to 1,
  36. and if false, that corresponds to 0.
  37. So if straight alone is true, that will give you a 4,
  38. and that's the right ranking for straight.
  39. If 5 alone is true, that will give you a 5, and that's the right ranking for flush.
  40. And if straight and flush are true, that will give you a 9.
  41. And what we had to do here, in the previous results
  42. straight flush was supposed to be 8, but it's okay if it's 9.
  43. All we have to do is take 5 of a kind and bump that up to 10,
  44. and now we get the right rankings by taking the maximum of that.
  45. We still have to return the ranks to break the ties,
  46. but now in 1 line we did what took 9 lines before,
  47. and you can decide is this what you like?
  48. It's nice and concise.
  49. Or do you like the previous approach because it was more explicit?
  50. And that's a question of taste. It's up to you which one you prefer.
  51. Now let's look at our lessons learned.
  52. We always start by understanding the problem.
  53. Look at the specification. See if it makes sense.
  54. If it doesn't make sense, talk to people.
  55. Try to figure it out. Consider different ways of trying to make sense of it.
  56. Then define the pieces of the problem.
  57. For our problem we had cards and hands and ranks and suits and so on.
  58. Figure out everything there is to represent in the problem.
  59. As much as you can, try to reuse the pieces that you have.
  60. We used the max function.
  61. We used the random shuffle function.
  62. Those were sort of standard pieces that we already knew about.
  63. Make sure you write tests.
  64. You don't know what you've done.
  65. You don't know what your program does unless you can specify it
  66. both in terms of the pieces that you're defining
  67. and in terms of the test that tells you what is it that this program does?
  68. And then explore the design space.
  69. We have many dimensions for the design space.
  70. We have to deal with correctness, with efficiency,
  71. with elegance, and with features.
  72. Decide where in the design space you want to be.
  73. Keep moving in the right direction, and use good taste to know when to stop.
  74. I hope we've learned something from this.
  75. Now let's take a look at some homework problems to really
  76. reinforce these lessons learned.