Got a YouTube account?

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

English subtitles

← Date Converter - Intro to Computer Science

Get Embed Code
4 Languages

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

  1. For this procedure, we're going to write a converter
  2. which takes a dictionary and a date as its inputs.
  3. The dictionary converts the number of the month to the name of the month,
  4. and the date is in the format month/day/year,
  5. which I've written up here so we don't forget the order.
  6. The output should be in the form day, name of the month, year.
  7. How are we going to split up month, day, and year?
  8. The way we're going to do that is to look for the forward slashes in the string.
  9. The first forward slash we can find at the position between the month and the day.
  10. Everything up to the first forward slash is the month.
  11. The month is equal to everything up to the first slash, and that's exclusive.
  12. It's doesn't include the slash itself.
  13. The second slash we can find by looking from the first slash onwards.
  14. We look at the position one after the first slash to find the second slash.
  15. We know it's somewhere there.
  16. We can't say it's exactly two steps after or one step,
  17. because the day could be one digit or two digits.
  18. The day is between the first slash and the second slash.
  19. Note, that we have to add 1 onto first because we want to go 1 position after the slash
  20. and it's inclusive for the first place here, and exclusive for the second.
  21. Then year is the date from 1 position after the second forward slash.
  22. So now we've got the day, the month, and the year.
  23. We need to turn it into the format to return it.
  24. We're going return the string which is day and the space, then the month, and then the year.
  25. Now month we need to look up in our dictionary.
  26. We're going to look it up in the dictionary, but we can't just put month in straight away,
  27. because month is a string and the dictionary uses a number as it's index.
  28. We have to turn the string into an integer.
  29. Then we need a space again, and finally, the year on the end.
  30. That's the first method.
  31. The second method is much shorter.
  32. It uses a procedure you've seen before but something that you may not know about that procedure.
  33. I'll show it to you just for completeness' sake.
  34. We can get the month, the day, and the year by splitting the date
  35. and using an input for that split.
  36. Now, split without an input just splits on white space.
  37. Split with input splits on whatever that input is.
  38. Date.split will split into month, day, and year.
  39. We can use a triple assignment to get those out straight away.
  40. Then we just need to return exactly the same as we returned before.
  41. That's a very simple way of writing it, which does exactly the same as the first one.