Филарета обнови решението на 14.03.2013 23:14 (преди над 11 години)
+from collections import defaultdict
+
+
+def groupby(func, seq):
+ keys = map(func, seq)
+ group_values = defaultdict(list)
+ iterator = iter(keys)
+ for item in seq:
+ group_values[next(iterator)].append(item)
+ return group_values
+
+
+def composer(f, g):
+ return lambda x: f(g(x))
+
+
+def iterate(func):
+ yield lambda x: x
+ composition = func
+ while True:
+ yield composition
+ composition = composer(func, composition)
+
+
+def zip_with(func, *iterables):
+ if not all([item is None for item in iterables]):
+ min_length = min(map(len, iterables))
+ for i in range(min_length):
+ yield func(*(map(lambda k: k[i], iterables)))
+
+
+def cache(func, cache_size):
+ def func_cached(*args):
+ if not hasattr(func, '_cache'):
+ func._cache = {}
+ if args in func._cache:
+ return func._cache[args]
+ new_value = func(*args)
+ func._cache[args] = new_value
+ return new_value
+ return func_cached
Има колекции, които не можеш да подадеш на len
:)