English subtitles

← Bending Numbers Solution - Programming Languages

Get Embed Code
3 Languages

Showing Revision 2 created 05/24/2016 by Udacity Robot.

  1. In this homework assignment, we asked you
  2. to do a few optimizations. The very basic
  3. optimizations you can do are very, very
  4. simple optimizations on binary operations, on
  5. basic arithmetic, and the ones we specifically
  6. asked you to do are these right here as well as
  7. replacing expressions with constants when
  8. applicable. So we gave you this one right here
  9. and then, to add in the others, we just do this
  10. great big ‘else… if…’ statement. So the first
  11. one, we check that the operation is times, we
  12. check that A is a number and it’s a number
  13. equal to one. And if it is, well, one times
  14. anything is that second thing, is B. So we just
  15. returned B. We don’t have to wait and run this
  16. through once we’re interpreting. That would
  17. take more effort than we need to. Now we do
  18. the same thing with the next one if B is one
  19. and A is something else. We can just return A.
  20. Now we can do the same thing with addition
  21. and 0. We just check if the operation is equal
  22. to plus and that A is a number and it’s a
  23. number equal to 0, then we return B. If B is a
  24. number equal to zero, then we return A. And
  25. finally, the last one that we asked you to
  26. implement is a number minus itself is 0. So
  27. that’s fairly straightforward too. Check if the
  28. operation’s equal to minus and then if A
  29. equals B – and remember, you can do that
  30. because you can check if Python tuples are
  31. equal to each other, not just numbers – then
  32. we return a tuple of number and 0 to indicate
  33. the number 0 in our abstract syntax tree. Now
  34. the constant folding is a little bit more work,
  35. but really not too much. First, we check that
  36. the first element of the tuples A and B are
  37. number. Then if the operation is addition, if
  38. the operation equals plus, then we return
  39. number and second element of A and B added
  40. together. So that way, we don’t have to run
  41. that operation later on when we are actually
  42. running the program. And we can do the same
  43. thing if we check if the operation is minus,
  44. then we run – then we return tuple of number
  45. and the second element of A and B subtracted
  46. and similarly for multiplication. We can just
  47. do the same thing for the second elements of
  48. A and B, multiply together. And then once we
  49. have hoped that the operations have been
  50. optimized in some way, we wrap it back up in
  51. a binary operation and a tuple of BinOp, AOP
  52. and B, and return that. And if none of this
  53. worked, if we didn’t even get a BinOp to
  54. begin with, then we just return the expression
  55. because those are all the optimizations that we
  56. have done. So let’s try that with a few of these
  57. test cases and these are the test cases that we
  58. provided you with. So we have zero, one and
  59. two, numbers equal to 0.0, 1.0 and 2.0, and a
  60. few variables that are ancient kings and
  61. queens of Persia and Macedonia – Xerxes,
  62. Darius, Antiochus and Musa – and then we’re
  63. going to define a plus operation, just so we
  64. don’t have to keep writing this tuple out, and a
  65. minus operation and a times operation
  66. similarly, just to save some key strokes. And
  67. then we’re going to check whether an
  68. expression that we’ve said, which is times 2
  69. and 0 is equal to 0. That is their optimization
  70. actually ran. We’re going to do the same thing
  71. with a minus operation and a slightly more
  72. complicated operation. Expression three is
  73. minus plus zero plus one plus two zero two.
  74. That was a quite a big mouthful, so let’s go
  75. through it just a bit more in-depth. Plus two
  76. and zero, so two plus zero and then we wrap it
  77. in this call to plus, so two plus zero plus one
  78. and we wrap it again in a plus zero, and then
  79. we wrap all of this up and subtract two from
  80. whatever this is. So this really should just be
  81. three minus two. Once you get past all of the
  82. massive amounts of parentheses. So we print
  83. out and make sure that that is indeed equal to
  84. one. We do the same thing for these
  85. expressions as well. So when we run this, we
  86. see that we pass all of our test cases. And this
  87. – our test cases involved a bunch of hand-
  88. checking, where we just made all these up and
  89. hand-checked that this is what they’re suppose
  90. to be and then verified that the optimization
  91. actually does what we think it’s going to do.
  92. This isn’t necessarily how you would
  93. normally do it, but it’s good enough to serve
  94. our purposes right now.