f | from itertools import cycle | f | from itertools import cycle |
| | | |
| def speed(path, stops, times): | | def speed(path, stops, times): |
n | distance_iter = iter(path) | n | path_iter = iter(path) |
| stop_sequence = cycle(stops) | | stops_iter = cycle(stops) |
| time_iter = iter(times) | | times_iter = iter(times) |
| while True: | | while True: |
n | report_stops = next(stop_sequence) | n | stop_count = next(stops_iter) |
| total_distance = 0 | | segment_distance = 0 |
| segments_count = 0 | | distances_covered = 0 |
| for _ in range(report_stops): | | for _ in range(stop_count): |
| segment = next(distance_iter, None) | | distance = next(path_iter, None) |
| if segment is None: | | if distance is None: |
| break | | break |
n | total_distance += segment | n | segment_distance += distance |
| segments_count += 1 | | distances_covered += 1 |
| if segments_count == 0: | | if distances_covered == 0: |
| return | | return |
n | travel_time = next(time_iter, None) | n | time = next(times_iter, None) |
| if travel_time is None: | | if time is None: |
| yield (total_distance / segments_count) | | yield (segment_distance / distances_covered) |
| return | | return |
t | if travel_time > 0: | t | if time > 0: |
| yield (total_distance / travel_time) | | yield (segment_distance / time) |