Решение на Четири функции от Николай Хубанов

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

Към профила на Николай Хубанов

Резултати

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

Код

from collections import deque
from itertools import groupby as groupby_
def groupby(func, iterable):
sorted_list = sorted(iterable, key=func)
return {key: list(group) for key, group in groupby_(sorted_list, func)}
def compose(f, g):
'Returns the composition f.g'
return lambda *args: f(g(*args))
def iterate(func):
result = lambda *args: args[0] if len(args) == 1 else args
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 if cache_size > 0 else func

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

......E.............
======================================================================
ERROR: test_groupby_no_repetitions (test.SecondHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20130408-29081-1e0t45i/test.py", line 14, in test_groupby_no_repetitions
    actual = solution.groupby(lambda x: x, [1, 2, 3, 5,'se7en'], )
  File "/tmp/d20130408-29081-1e0t45i/solution.py", line 6, in groupby
    sorted_list = sorted(iterable, key=func)
TypeError: unorderable types: str() < int()

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

FAILED (errors=1)

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

Николай обнови решението на 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

Николай обнови решението на 15.03.2013 16:47 (преди над 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 groupby(func, iterable):
+ sorted_list = sorted(iterable, key=func)
+ return {key: list(group) for key, group in groupby_(sorted_list, func)}
def compose(f, g):
+ 'Returns the composition f.g'
return lambda *args: f(g(*args))
def iterate(func):
- result = lambda x: x
+ result = lambda *args: args[0] if len(args) == 1 else args
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
+ return cached_version if cache_size > 0 else func