Решение на Четири функции от Милан Миланов

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

Към профила на Милан Миланов

Резултати

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

Код

from collections import defaultdict, deque
def groupby(func, seq):
grouped_elements = defaultdict(list)
for element in seq:
grouped_elements[func(element)].append(element)
return dict(grouped_elements)
def iterate(func):
def compose(func1, func2):
return lambda arg: func1(func2(arg))
current_func = lambda arg: arg
while True:
yield current_func
current_func = compose(func, current_func)
def zip_with(func, *iterables):
for args in zip(*iterables):
yield func(*args)
def cache(func, cache_size):
cache = deque(maxlen=cache_size)
def func_cached(*args):
for cached_args, cached_value in cache:
if cached_args == args:
return cached_value
result = func(*args)
cache.append((args, result))
return result
return func_cached

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

....................
----------------------------------------------------------------------
Ran 20 tests in 0.014s

OK

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

Милан обнови решението на 14.03.2013 00:13 (преди над 11 години)

+from collections import defaultdict, deque
+
+
+def groupby(func, seq):
+ grouped_elements = defaultdict(list)
+
+ for element in seq:
+ grouped_elements[func(element)].append(element)
+
+ return dict(grouped_elements)
+
+
+def iterate(func):
+ def compose(f1, f2):
+ return lambda *args, **kwargs: f1(f2(*args, **kwargs))
+
+ current_func = lambda arg: arg
+
+ while True:
+ yield current_func
+ current_func = compose(func, current_func)
+
+
+def zip_with(func, *iterables):
+ if iterables:
+ for i in range(0, min(map(len, iterables))):
+ yield func(*[iterable[i] for iterable in iterables])
+
+
+def cache(func, cache_size):
+ cache = deque(maxlen=cache_size)
+
+ def func_cached(*args):
+ for cached_args, cached_value in cache:
+ if cached_args == args:
+ return cached_value
+ else:
+ answer = func(*args)
+ cache.append((args, answer))
+ return answer
+
+ return func_cached

Милан обнови решението на 14.03.2013 00:49 (преди над 11 години)

from collections import defaultdict, deque
def groupby(func, seq):
grouped_elements = defaultdict(list)
for element in seq:
grouped_elements[func(element)].append(element)
return dict(grouped_elements)
def iterate(func):
def compose(f1, f2):
return lambda *args, **kwargs: f1(f2(*args, **kwargs))
current_func = lambda arg: arg
while True:
yield current_func
current_func = compose(func, current_func)
def zip_with(func, *iterables):
if iterables:
for i in range(0, min(map(len, iterables))):
yield func(*[iterable[i] for iterable in iterables])
def cache(func, cache_size):
cache = deque(maxlen=cache_size)
def func_cached(*args):
for cached_args, cached_value in cache:
if cached_args == args:
return cached_value
- else:
- answer = func(*args)
- cache.append((args, answer))
- return answer
+
+ answer = func(*args)
+ cache.append((args, answer))
+ return answer
return func_cached

Милан обнови решението на 15.03.2013 13:42 (преди над 11 години)

from collections import defaultdict, deque
def groupby(func, seq):
grouped_elements = defaultdict(list)
for element in seq:
grouped_elements[func(element)].append(element)
return dict(grouped_elements)
def iterate(func):
- def compose(f1, f2):
- return lambda *args, **kwargs: f1(f2(*args, **kwargs))
+ def compose(func1, func2):
+ return lambda arg: func1(func2(arg))
current_func = lambda arg: arg
while True:
yield current_func
current_func = compose(func, current_func)
def zip_with(func, *iterables):
if iterables:
- for i in range(0, min(map(len, iterables))):
- yield func(*[iterable[i] for iterable in iterables])
+ for args in zip(*iterables):
+ yield func(*args)
def cache(func, cache_size):
cache = deque(maxlen=cache_size)
def func_cached(*args):
for cached_args, cached_value in cache:
if cached_args == args:
return cached_value
- answer = func(*args)
- cache.append((args, answer))
- return answer
+ result = func(*args)
+ cache.append((args, result))
+ return result
return func_cached

Милан обнови решението на 15.03.2013 18:49 (преди над 11 години)

from collections import defaultdict, deque
def groupby(func, seq):
grouped_elements = defaultdict(list)
for element in seq:
grouped_elements[func(element)].append(element)
return dict(grouped_elements)
def iterate(func):
def compose(func1, func2):
return lambda arg: func1(func2(arg))
current_func = lambda arg: arg
while True:
yield current_func
current_func = compose(func, current_func)
def zip_with(func, *iterables):
- if iterables:
- for args in zip(*iterables):
- yield func(*args)
+ for args in zip(*iterables):
+ yield func(*args)
def cache(func, cache_size):
cache = deque(maxlen=cache_size)
def func_cached(*args):
for cached_args, cached_value in cache:
if cached_args == args:
return cached_value
result = func(*args)
cache.append((args, result))
return result
return func_cached