April 2018
Intermediate to advanced
408 pages
10h 42m
English
If we want to implement this without using the Counter class, we can use a more functional approach of sorting and grouping. Following is a common algorithm:
from typing import Dict, Any, Iterable, Tuple, List, TypeVarLeg = Tuple[Any, Any, float]T_ = TypeVar("T_")def group_sort1(trip: Iterable[Leg]) -> Dict[int, int]: def group( data: Iterable[T_] ) -> Iterable[Tuple[T_, int]]: previous, count = None, 0 for d in sorted(data): if d == previous: count += 1 elif previous is not None: # and d != previous yield previous, count previous, count = d, 1 elif previous is None: previous, count = d, 1 else: raise Exception("Bad bad design problem.") yield previous, count quantized = (int(5*(dist//5)) for beg, end, dist in ...