Георги обнови решението на 13.03.2013 00:02 (преди над 11 години)
+from collections import defaultdict, OrderedDict
+
+
+def groupby(func, seq):
+ """ Group values in seq on keys generated by func and return a dict. """
+ grouped_items = defaultdict(list)
+
+ for item in seq:
+ grouped_items[func(item)].append(item)
+
+ return dict(grouped_items)
+
+
+def compose(func1, func2):
+ """ Return the composition of two functions. """
+ return lambda arg: func1(func2(arg))
+
+
+def iterate(func):
+ """Return the iteration of a function.
+
+ Generate infinite sequence of compositions of func. The nth element in the
+ sequence is the composition of (n - 1) functions.
+
+ The first element is the identity function.
+
+ """
+ current_composition_func = lambda x: x
+
+ while True:
+ yield current_composition_func
+ current_composition_func = compose(func, current_composition_func)
+
+
+def zip_with(func, *iterables):
+ """Generate sequence of elements where the nth element is the result of
+ func applied to the nth elements of all iterables.
+
+ """
+ iterators = [iter(iterable) for iterable in iterables]
+
+ while True:
+ values = [next(iterator) for iterator in iterators]
+ yield func(*values)
+
+
+def cache(func, cache_size):
+ """Return a function that caches the last `cache_size` results of `func`.
+
+ """
+ cache_store = OrderedDict()
+
+ def cached_func(*args):
+ if args in cache_store:
+ return cache_store[args]
+
+ if len(cache_store) == cache_size:
+ cache_store.popitem(False)
+
+ cache_store[args] = func(*args)
+ return cache_store[args]
+
+ return cached_func
Много добро решение.