Решение на Четири функции от Ния Дончева

Обратно към всички решения

Към профила на Ния Дончева

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 20 успешни тест(а)
  • 0 неуспешни тест(а)

Код

from collections import deque
def groupby(func, seq):
return {func(x):
[item for item in seq if func(item) == func(x)]
for x in seq}
def composition(func_1, func_2):
return lambda x: func_1(func_2(x))
def iterate(func):
result = lambda x: x
while True:
yield result
result = composition(func, result)
def zip_with(func, *iterables):
i = zip(*iterables)
while True:
yield func(*next(i))
def cache(func, cache_size):
values = {}
cache_order = deque()
def func_cashed(*args):
if args in values:
return values[args]
result = func(*args)
if cache_size:
if len(values) == cache_size:
values.pop(cache_order.popleft())
cache_order.append(args)
values[args] = result
return result
return func_cashed

Лог от изпълнението

....................
----------------------------------------------------------------------
Ran 20 tests in 0.010s

OK

История (1 версия и 0 коментара)

Ния обнови решението на 15.03.2013 18:31 (преди около 11 години)

+from collections import deque
+
+
+def groupby(func, seq):
+ return {func(x):
+ [item for item in seq if func(item) == func(x)]
+ for x in seq}
+
+
+def composition(func_1, func_2):
+ return lambda x: func_1(func_2(x))
+
+
+def iterate(func):
+ result = lambda x: x
+
+ while True:
+ yield result
+ result = composition(func, result)
+
+
+def zip_with(func, *iterables):
+ i = zip(*iterables)
+
+ while True:
+ yield func(*next(i))
+
+
+def cache(func, cache_size):
+ values = {}
+ cache_order = deque()
+
+ def func_cashed(*args):
+ if args in values:
+ return values[args]
+
+ result = func(*args)
+
+ if cache_size:
+ if len(values) == cache_size:
+ values.pop(cache_order.popleft())
+ cache_order.append(args)
+ values[args] = result
+
+ return result
+ return func_cashed