Милан обнови решението на 14.03.2013 00:13 (преди над 11 години)
+from collections import defaultdict, deque
+
+
+def groupby(func, seq):
+ grouped_elements = defaultdict(list)
+
+ for element in seq:
+ grouped_elements[func(element)].append(element)
+
+ return dict(grouped_elements)
+
+
+def iterate(func):
+ def compose(f1, f2):
+ return lambda *args, **kwargs: f1(f2(*args, **kwargs))
+
+ current_func = lambda arg: arg
+
+ while True:
+ yield current_func
+ current_func = compose(func, current_func)
+
+
+def zip_with(func, *iterables):
+ if iterables:
+ for i in range(0, min(map(len, iterables))):
+ yield func(*[iterable[i] for iterable in iterables])
+
+
+def cache(func, cache_size):
+ cache = deque(maxlen=cache_size)
+
+ def func_cached(*args):
+ for cached_args, cached_value in cache:
+ if cached_args == args:
+ return cached_value
+ else:
+ answer = func(*args)
+ cache.append((args, answer))
+ return answer
+
+ return func_cached