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

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

Към профила на Мария Терзиева

Резултати

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

Код

from collections import defaultdict, OrderedDict
def groupby(func, seq):
result = defaultdict(list)
for element in seq:
result[func(element)].append(element)
return result
def iterate(func):
def repeated(n, arg):
for i in range(n):
arg = func(arg)
return arg
repeat = 0
while True:
yield lambda arg: repeated(repeat, arg)
repeat += 1
def zip_with(func, *iterables):
for args in zip(*iterables):
yield func(*args)
def cache(func, cache_size):
cache_storage = OrderedDict()
def check_and_modify_cache(arg):
if arg in cache_storage:
return cache_storage[arg]
else:
result = cache_storage[arg] = func(arg)
if len(cache_storage) > cache_size:
cache_storage.popitem(last=False)
return result
return lambda arg: check_and_modify_cache(arg)

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

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

======================================================================
FAIL: 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-yx0f0m/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: 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-yx0f0m/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: 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-yx0f0m/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


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

FAILED (failures=3, errors=1)

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

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

+from collections import defaultdict, OrderedDict
+
+
+def groupby(func, seq):
+ result = defaultdict(list)
+ for element in seq:
+ result[func(element)].append(element)
+ return result
+
+
+def iterate(func):
+ def repeated(n, arg):
+ for i in range(n):
+ arg = func(arg)
+ return arg
+ repeat = 0
+ while True:
+ yield lambda arg: repeated(repeat, arg)
+ repeat += 1
+
+
+def zip_with(func, *iterables):
+ for args in zip(*iterables):
+ yield func(*args)
+
+
+def cache(func, cache_size):
+ cache_storage = OrderedDict()
+
+ def check_and_modify_cache(arg):
+ if arg in cache_storage:
+ return cache_storage[arg]
+ else:
+ result = cache_storage[arg] = func(arg)
+ if len(cache_storage) > cache_size:
+ cache_storage.popitem(last=False)
+ return result
+
+ return lambda arg: check_and_modify_cache(arg)