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

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

Към профила на Мартина Величкова

Резултати

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

Код

from itertools import starmap
from collections import OrderedDict
def groupby(func, seq):
result = {}
for iter in seq:
result.setdefault(func(iter), []).append(iter)
return result
def zip_with(func, *iterables):
return starmap(func, zip(*iterables))
class Cache:
def __init__(self, func, cache_size):
self.cache = OrderedDict()
self.func = func
self.cache_size = cache_size
def func_cached(self, *args):
if args in self.cache:
return self.cache[args]
else:
if len(self.cache) == self.cache_size:
self.cache.popitem(False)
result = self.func(*args)
self.cache[args] = result
return result
def cache(func, cache_size):
cached = Cache(func, cache_size)
return cached.func_cached

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

...E......EEEEE.....
======================================================================
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-1yeyxvd/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1yeyxvd/solution.py", line 27, in func_cached
    self.cache.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'

======================================================================
ERROR: test_iterate_ordered_calls (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-1yeyxvd/test.py", line 37, in test_iterate_ordered_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: test_iterate_out_of_order_calls (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-1yeyxvd/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: test_iterate_out_of_order_calls_again (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-1yeyxvd/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: test_iterate_out_of_order_calls_yet_again (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-1yeyxvd/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: test_iterate_start_with_identity_function (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-1yeyxvd/test.py", line 32, in test_iterate_start_with_identity_function
    bracketisers = solution.iterate(lambda x: '(' + x + ')') # there's no such word, really
AttributeError: 'module' object has no attribute 'iterate'

----------------------------------------------------------------------
Ran 20 tests in 0.013s

FAILED (errors=6)

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

Мартина обнови решението на 15.03.2013 22:47 (преди над 11 години)

+from itertools import starmap
+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ result = {}
+ for iter in seq:
+ result.setdefault(func(iter), []).append(iter)
+ return result
+
+
+def zip_with(func, *iterables):
+ return starmap(func, zip(*iterables))
+
+
+class Cache:
+ def __init__(self, func, cache_size):
+ self.cache = OrderedDict()
+ self.func = func
+ self.cache_size = cache_size
+
+ def func_cached(self, *args):
+ if args in self.cache:
+ return self.cache[args]
+ else:
+ if len(self.cache) == self.cache_size:
+ self.cache.popitem(False)
+ result = self.func(*args)
+ self.cache[args] = result
+ return result
+
+
+def cache(func, cache_size):
+ cached = Cache(func, cache_size)
+ return cached.func_cached