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

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

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

Резултати

  • 5 точки от тестове
  • 1 отнета точка
  • 4 точки общо
  • 17 успешни тест(а)
  • 3 неуспешни тест(а)

Код

from collections import OrderedDict
def groupby(func, seq):
dict_result = {}
for elem in seq:
dict_result.setdefault(func(elem), []).append(elem)
return dict_result
def iterate(func):
f = lambda x: x
compose = lambda y: lambda x: func(y(x))
while True:
yield f
f = compose(f)
def zip_with(func, *iterables):
return [func(*args) for args in zip(*iterables)]
def cache(func, cache_size):
stored = OrderedDict()
def func_cached(arg):
if arg not in stored:
result=func(arg)
if len(stored) == cache_size:
stored.popitem(False)
stored[arg] = result
return result
return func_cached

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

.FEE................
======================================================================
ERROR: test_cache_function_with_vargs (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-thch46/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cached() takes 1 positional argument but 3 were given

======================================================================
ERROR: test_cache_no_cache (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-thch46/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-thch46/solution.py", line 31, in func_cached
    stored.popitem(False)
  File "/opt/python3.3/lib/python3.3/collections/__init__.py", line 114, in popitem
    raise KeyError('dictionary is empty')
KeyError: 'dictionary is empty'

======================================================================
FAIL: test_cache_call_is_cached (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-thch46/test.py", line 118, in test_cache_call_is_cached
    self.assertEqual(256, cached_double(128))
AssertionError: 256 != None

----------------------------------------------------------------------
Ran 20 tests in 0.017s

FAILED (failures=1, errors=2)

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

Гергана обнови решението на 15.03.2013 23:52 (преди над 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ dict_result = {}
+ for elem in seq:
+ dict_result.setdefault(func(elem), []).append(elem)
+ return dict_result
+
+
+def iterate(func):
+ f = lambda x: x
+ compose = lambda y: lambda x: func(y(x))
+ while True:
+ yield f
+ f = compose(f)
+
+
+def zip_with(func, *iterables):
+ return [func(*args) for args in zip(*iterables)]
+
+
+def cache(func, cache_size):
+
+ stored = OrderedDict()
+
+ def func_cached(arg):
+ if arg not in stored:
+ result=func(arg)
+ if len(stored) == cache_size:
+ stored.popitem(False)
+ stored[arg] = result
+ return result
+ return func_cached