Решение на Четири функции от Борис Пелтеков

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

Към профила на Борис Пелтеков

Резултати

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

Код

from collections import defaultdict
from itertools import count
def groupby(func, seq):
res = defaultdict(list)
for i in seq:
res[func(i)].append(i)
return res;
def helper(f, i):
if i == 0:
return lambda x:x
else:
return lambda x:f(helper(f, i-1)(x))
def iterate(f):
i = 0
while True:
yield helper(f, i)
i = i + 1
def zip_with(func, *iterables):
i = 0
iterators = []
for it in iterables:
iterators.append(iter(it))
while True:
try:
res = []
for it in iterators:
res.append(next(it))
yield func(*res)
except StopIteration:
break

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

EEEEE..........E....
======================================================================
ERROR: test_cache_cache_is_not_global (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-1rq7uco/test.py", line 129, in test_cache_cache_is_not_global
    cached_double1 = solution.cache(double, 3)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: 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-1rq7uco/test.py", line 116, in test_cache_call_is_cached
    cached_double = solution.cache(double, 10)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
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-1rq7uco/test.py", line 159, in test_cache_function_with_vargs
    cached_sum = solution.cache(sum_varargs, 10)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
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-1rq7uco/test.py", line 103, in test_cache_no_cache
    cached_double = solution.cache(double, 0)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: test_cache_size_is_respected (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-1rq7uco/test.py", line 143, in test_cache_size_is_respected
    cached_double = solution.cache(double, 2)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: test_zip_with_empty (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-1rq7uco/test.py", line 67, in test_zip_with_empty
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1rq7uco/solution.py", line 32, in zip_with
    yield func(*res)
TypeError: <lambda>() missing 1 required positional argument: 'x'

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

FAILED (errors=6)

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

Борис обнови решението на 15.03.2013 23:58 (преди около 11 години)

+from collections import defaultdict
+from itertools import count
+
+def groupby(func, seq):
+ res = defaultdict(list)
+ for i in seq:
+ res[func(i)].append(i)
+ return res;
+
+def helper(f, i):
+ if i == 0:
+ return lambda x:x
+ else:
+ return lambda x:f(helper(f, i-1)(x))
+
+def iterate(f):
+ i = 0
+ while True:
+ yield helper(f, i)
+ i = i + 1
+
+def zip_with(func, *iterables):
+ i = 0
+ iterators = []
+ for it in iterables:
+ iterators.append(iter(it))
+ while True:
+ try:
+ res = []
+ for it in iterators:
+ res.append(next(it))
+ yield func(*res)
+ except StopIteration:
+ break