English subtitles

← Algorithm Pseudocode - Intro to Computer Science

Get Embed Code
2 Languages

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

  1. Let's try to write down an algorithm that systematizes how we solve it. And
  2. we're going to write this as pseudocode. That means we're not worrying about the
  3. details of how to write exactly correct Python. We're trying to get the idea
  4. down and see if it makes sense. So let's think about how we solve the problem as
  5. a human again. Here's how we start it. We looked at our start date, which was
  6. January 24. And we counted the number of days remaining in January. We do that
  7. by just counting up. If we started earlier we might not want to have counted
  8. every day. So we can subtract the total number of days in January from the day
  9. we started. So that will be our starting point. And we're going to keep the
  10. results in a variable called days and we'll start by initializing days into the
  11. number of days in the card line minus the day that we're starting on. So, for
  12. the example, that was 31, for the number of days in January, minus 24, which, I
  13. believe is 7. And once we've done that, well, we finished month1, so we should
  14. increase the value of month1 by 1. So now we were at February and now we started
  15. going through the months, until we got to the month we are on. So we went
  16. through each month until we got up to the month we were looking for, which was
  17. June. So that's sort of like a while loop. We're doing something over and over
  18. again until we get to the stopping condition. So we can write that as
  19. pseudocode. We're going to keep going while the current month is less than the
  20. target month. We're going to add the number of days in the current month. And
  21. we're going to increase the current month by 1. Now, we're not quite done we
  22. still need to account for the day in the month that we arrived at. And there we
  23. just add that day so if it was day one of June then we would add one. If we
  24. didn't actually get in to June. Now, we're going to add 29 since the 29th was
  25. our target day. We haven't yet accounted for years, for this example it was
  26. okay. But, for other cases, we're going to need to account for years, so let's
  27. think about how to do that. We can do it similarly to how we accounted for
  28. month. Well, we're not up to the same year. We're going to add the number of
  29. days in that year.