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

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

Към профила на Десислава Добрева

Резултати

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

Код

import functools
def groupby (func, seq):
return {func(x) : [y for y in seq if func(x) == func(y)] for x in seq}
def iterate(func):
def help_func(arg, _help=1):
for i in range(_help):
arg = func(arg)
return arg
count = 0
while True:
yield functools.partial(help_func, _help=count)
count += 1
def zip_with(func, *iterables):
for itr in zip(*iterables):
yield func(*itr)
def cache(func, cache_size):
cached_args_list = []
cached_results_list = []
def func_cached(*args):
if cache_size > 0:
if args not in cached_args_list:
cached_args_list.append(args)
cached_results_list.append(func(*args))
if len(cached_args_list) > cache_size:
cached_args_list.pop(0)
cached_results_list.pop(0)
return cached_results_list[cached_args_list.index(args)]
else:
return func(*args)
return func_cached

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

....................
----------------------------------------------------------------------
Ran 20 tests in 0.012s

OK

История (2 версии и 3 коментара)

Десислава обнови решението на 14.03.2013 23:25 (преди над 11 години)

+import functools
+
+#groupby
+def groupby (func, seq):
+ return {i: list(filter(lambda x: func(seq[x]) == i, seq)) for i in list(map(func, seq))}
+
+#iterate
+def iterate(func):
+ def help_func(arg, _help=1):
+ for i in range(_help):
+ arg = func(arg)
+ return arg
+ count = 0
+ while True:
+ yield functools.partial(help_func, _help=count)
+ count += 1
+
+#zip_with
+def zip_with(func, *iterables):
+ for itr in zip(*iterables):
+ yield func(*itr)
+
+#cache
+def cache(func, cache_size):
+ cached_args_list = []
+ cached_results_list = []
+ def func_cached(*args):
+ if cache_size > 0:
+ if args not in cached_args_list:
+ cached_args_list.append(args)
+ cached_results_list.append(func(*args))
+ if len(cached_args_list) > cache_size:
+ cached_args_list.pop(0)
+ cached_results_list.pop(0)
+ return cached_results_list[cached_args_list.index(args)]
+ else:
+ return func(*args)
+ return func_cached

Десислава обнови решението на 15.03.2013 09:12 (преди над 11 години)

import functools
-#groupby
def groupby (func, seq):
- return {i: list(filter(lambda x: func(seq[x]) == i, seq)) for i in list(map(func, seq))}
+ return {func(x) : [y for y in seq if func(x) == func(y)] for x in seq}
-#iterate
def iterate(func):
def help_func(arg, _help=1):
for i in range(_help):
arg = func(arg)
return arg
count = 0
while True:
yield functools.partial(help_func, _help=count)
count += 1
-#zip_with
def zip_with(func, *iterables):
for itr in zip(*iterables):
yield func(*itr)
-#cache
def cache(func, cache_size):
cached_args_list = []
cached_results_list = []
def func_cached(*args):
if cache_size > 0:
if args not in cached_args_list:
cached_args_list.append(args)
cached_results_list.append(func(*args))
if len(cached_args_list) > cache_size:
cached_args_list.pop(0)
cached_results_list.pop(0)
return cached_results_list[cached_args_list.index(args)]
else:
return func(*args)
- return func_cached
+ return func_cached
80406.py:3:1: E302 expected 2 blank lines, found 1
80406.py:3:12: E211 whitespace before '('
80406.py:4:20: E203 whitespace before ':'
80406.py:6:1: E302 expected 2 blank lines, found 1
80406.py:16:1: E302 expected 2 blank lines, found 1
80406.py:17:4: E111 indentation is not a multiple of four
80406.py:18:8: E111 indentation is not a multiple of four
80406.py:20:1: E302 expected 2 blank lines, found 1
80406.py:23:5: E301 expected 1 blank line, found 0
80406.py:29:44: W291 trailing whitespace
80406.py:30:47: W291 trailing whitespace