Advertisement
Guest User

Untitled

a guest
Dec 16th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. def time_of_trip(datum, city):
  2.     """
  3.    Takes as input a dictionary containing info about a single trip (datum) and
  4.    its origin city (city) and returns the month, hour, and day of the week in
  5.    which the trip was made.
  6.    
  7.    Remember that NYC includes seconds, while Washington and Chicago do not.
  8.    
  9.    HINT: You should use the datetime module to parse the original date
  10.    strings into a format that is useful for extracting the desired information.
  11.    see https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior
  12.    """
  13.    
  14.     if city == 'NYC':
  15.         start_date = datum['starttime']
  16.         trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M:%S")
  17.         trip_date_format = trip_date.strftime("%m, %H, %A")
  18.         return trip_date_format
  19.    
  20.     elif city == 'Chicago':
  21.         start_date = datum['starttime']
  22.         trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M")
  23.         trip_date_format = trip_date.strftime("%m, %H, %A")
  24.         return trip_date_format
  25.        
  26.     elif city == 'Washington':
  27.         start_date = datum['Start date']
  28.         trip_date = datetime.strptime(start_date, "%m/%d/%Y %H:%M")
  29.         trip_date_format = trip_date.strftime("%m, %H, %A")
  30.         return trip_date_format
  31.    
  32.  
  33. # Some tests to check that your code works. There should be no output if all of
  34. # the assertions pass. The `example_trips` dictionary was obtained from when
  35. # you printed the first trip from each of the original data files.
  36. tests = {'NYC': (1, 0, 'Friday'),
  37.          'Chicago': (3, 23, 'Thursday'),
  38.          'Washington': (3, 22, 'Thursday')}
  39.  
  40. for city in tests:
  41.     assert time_of_trip(example_trips[city], city) == tests[city]
  42.     print(example_trips[city], city)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement