Решение на Четири функции от Иван Капукаранов

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

Към профила на Иван Капукаранов

Резултати

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

Код

def groupby(func, seq):
storage = {}
z = map(func, seq)
for i in seq:
storage.setdefault(next(z), []).append(i)
return storage
def iterate(func):
def identity(p):
return p
def composition(f1, f2):
return (lambda x: f1(f2(x)))
while True:
yield identity
identity = composition(func, identity)
def zip_with(func, *iterables):
ziped = zip(*iterables)
while True:
zip_it = next(ziped)
yield func(*zip_it)
def cache(func, cache_size):
results = {}
def func_cashed(x):
if not x in results:
results[x] = func(x)
if (len(results) > cache_size):
results.popitem()
return results[x]
return func_cashed

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

..EE................
======================================================================
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-1qx6pyi/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cashed() 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-1qx6pyi/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1qx6pyi/solution.py", line 35, in func_cashed
    return results[x]
KeyError: 42

----------------------------------------------------------------------
Ran 20 tests in 0.011s

FAILED (errors=2)

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

Иван обнови решението на 15.03.2013 20:35 (преди около 11 години)

+def groupby(func, seq):
+ storage = {}
+ z = map(func, seq)
+ for i in seq:
+ storage.setdefault(next(z), []).append(i)
+ return storage
+
+
+def iterate(func):
+ def identity(p):
+ return p
+
+ def composition(f1, f2):
+ return (lambda x: f1(f2(x)))
+ while True:
+ yield identity
+ identity = composition(func, identity)
+
+
+def zip_with(func, *iterables):
+ ziped = zip(*iterables)
+ while True:
+ zip_it = next(ziped)
+ yield func(*zip_it)
+
+
+def cache(func, cache_size):
+ results = {}
+
+ def func_cashed(x):
+ if not x in results:
+ results[x] = func(x)
+ if (len(results) > cache_size):
+ results.popitem()
+ return results[x]
+ return func_cashed