t | from itertools import cycle, islice | t | from itertools import cycle, islice |
| | | |
| def speed(path, stops, times): | | def speed(path, stops, times): |
| stops_cycle = cycle(stops) | | stops_cycle = cycle(stops) |
| path_iter = iter(path) | | path_iter = iter(path) |
| time_iter = iter(times) | | time_iter = iter(times) |
| while True: | | while True: |
| stop_count = next(stops_cycle) | | stop_count = next(stops_cycle) |
| distance = sum(islice(path_iter, stop_count)) | | distance = sum(islice(path_iter, stop_count)) |
| if distance == 0: | | if distance == 0: |
| break | | break |
| time_spent = next(time_iter) | | time_spent = next(time_iter) |
| yield (distance / time_spent) | | yield (distance / time_spent) |