Георги обнови решението на 14.03.2013 22:36 (преди над 11 години)
+from functools import lru_cache
+
+
+def groupby(func, seq):
+
+ grouping_result = {}
+
+ for value in seq:
+ func_result = func(value)
+
+ if func_result in grouping_result:
+ grouping_result[func_result] += [value]
+ else:
+ grouping_result[func_result] = [value]
+
+ return grouping_result
+
+
+def iterate(func):
+
+ def compose(outter_func, inner_func):
+ return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
+
+ returned_func = lambda x: x
+
+ while True:
+ yield returned_func
+ returned_func = compose(func, returned_func)
+
+
+def zip_with(func, *iterables):
+
+ if iterables:
+ iterables_min_len = len(iterables[0])
+ else:
+ iterables_min_len = 0
+
+ for iterable in iterables:
+ if iterables_min_len > len(iterable):
+ iterables_min_len = len(iterable)
+
+ for argument_position in range(iterables_min_len):
+ new_args = []
+ for iterable in iterables:
+ new_args.append(iterable[argument_position])
+
+ yield func(*new_args)
+
+
+def cache(func, cache_size):
+
+ @lru_cache(maxsize=cache_size)
+ def func_cached(number):
+ return func(number)
+
+ return func_cached
Идеята беше вие да си напишете кеширащата функция :)