Николай обнови решението на 14.03.2013 03:01 (преди над 11 години)
+from collections import deque
+from itertools import groupby as groupby_
+
+
+def groupby(func, seq):
+ sorted_seq = sorted(seq, key=func)
+ return {key: list(group) for key, group in groupby_(sorted_seq, func)}
+
+
+def compose(f, g):
+ return lambda *args: f(g(*args))
+
+
+def iterate(func):
+ result = lambda x: x
+ while True:
+ yield result
+ result = compose(func, result)
+
+
+def zip_with(func, *iterables):
+ return (func(*zipped_args) for zipped_args in zip(*iterables))
+
+
+def cache(func, cache_size):
+ cached_results = {}
+ ordered_args = deque()
+
+ def cached_version(*args):
+ if args not in cached_results:
+ if len(ordered_args) == cache_size:
+ removed_args = ordered_args.popleft()
+ del cached_results[removed_args]
+
+ ordered_args.append(args)
+ cached_results[args] = func(*args)
+
+ return cached_results[args]
+
+ return cached_version
Какво значи seq
?
popleft
и len
са неприложими в някои случаи. Можеш ли да се сетиш в кои?