YouTube

Got a YouTube account?

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

English subtitles

← Foxes And Hens Solution - Design of Computer Programs

Get Embed Code
1 Language

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. Here's my answer. Here's the definition for the do function.
  2. I make a random choice of the cards,
  3. and I replace that card in the deck with the empty string.
  4. I do that once, and that gives me the cards left without that 1 card.
  5. Then if the action is gathering, I add to my score the number of hens that were in the yard,
  6. and there's no more hens left in the yard.
  7. If the action was waiting and a hen shows up,
  8. then I add that to the number of hens in the yard.
  9. And if the action was waiting and a fox shows up,
  10. then, oops, I don't add anything to my score and the yard goes down to 0,
  11. and I've still got those cards left.
  12. Then if something went wrong, if the strategy function returned a bad value or something,
  13. then I just don't do anything and I return the state.
  14. And since we didn't specify what to do, you're free to do what you want in that case.
  15. You could have raised an error. That would be a reasonable thing to do.
  16. But the easiest is just to return the state.
  17. Here's my strategy function.
  18. I didn't bother writing out the optimal function, although you certainly could do that.
  19. What I did is first I said, "We're counting cards."
  20. "We better take advantage of that to some degree."
  21. "If there are no foxes left, then we might as well wait
  22. "and gather up all the rest of the chickens."
  23. So I made sure I did that.
  24. And then I tried all possible values of how long we should wait for
  25. and found that 3 was the best.
  26. So we wait until there's 3, then we gather them,
  27. and if there's less than 3, then we wait.
  28. Is this strategy superior? The answer is yes, it is.
  29. By how much? By almost 3 points.
  30. Take5 averaged 29.57, and my strategy averaged 32.3.