Problem 435682 · easy · Level 04 Non-Linear Data Structures

A Timetable You Can Read Twice

py-protocols · py-dunder · iteration protocol · iterables

A bus leaves the depot at first and then every every minutes, as long as the departure time is not later than last. Times are strings "HH:MM" on one day.

Write a class Timetable(first, last, every) whose objects work with Python's built-in tools:

  • looping over a timetable gives the departure times in order, as "HH:MM" strings, and it can be looped over as often as you like, including two loops over the same timetable at the same time;
  • len(t) is the number of departures;
  • time in t says whether a bus leaves at time.

The tests use your class with ordinary built-ins (list, len, in, max, zip) and with two small helpers from the setup that only loop over what they are given: gaps(t) lists the minutes between neighbouring departures, and meet(a, b, window) counts the pairs of departures from two timetables at most window minutes apart.

Examples

Input:  t = Timetable("08:00", "09:00", 25)
        list(t), len(t), "08:50" in t, "08:51" in t
Output: (['08:00', '08:25', '08:50'], 3, True, False)

Input:  gaps(Timetable("23:30", "23:59", 10))
Output: [10, 10]

Constraints

  • "00:00" <= first, last <= "23:59", 1 <= every <= 1440; if first is later than last, there are no departures.
  • len must not loop over the departures (the tests call it often on long timetables).

Goals

  • Make an object usable in `for`, `list`, `zip` and `in` by giving it `__iter__`
  • Return a fresh iterator on every call, so that the object can be looped over many times
  • Add `__len__` computed from the object's state rather than by counting
Starting Python…