Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def time_of_trip(datum, city):
- """
- Takes as input a dictionary containing info about a single trip (datum) and
- its origin city (city) and returns the month, hour, and day of the week in
- which the trip was made.
- Remember that NYC includes seconds, while Washington and Chicago do not.
- HINT: You should use the datetime module to parse the original date
- strings into a format that is useful for extracting the desired information.
- see https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
- """
- if city == 'NYC':
- start_date = datum['starttime']
- trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M:%S")
- trip_date_format = trip_date.strftime("%m, %H, %A")
- return trip_date_format
- elif city == 'Chicago':
- start_date = datum['starttime']
- trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M")
- trip_date_format = trip_date.strftime("%m, %H, %A")
- return trip_date_format
- elif city == 'Washington':
- start_date = datum['Start date']
- trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M")
- trip_date_format = trip_date.strftime("%m, %H, %A")
- return trip_date_format
- # Some tests to check that your code works. There should be no output if all of
- # the assertions pass. The `example_trips` dictionary was obtained from when
- # you printed the first trip from each of the original data files.
- tests = {'NYC': (1, 0, 'Friday'),
- 'Chicago': (3, 23, 'Thursday'),
- 'Washington': (3, 22, 'Thursday')}
- for city in tests:
- assert time_of_trip(example_trips[city], city) == tests[city]
- print(example_trips[city], city)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement