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

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

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

Резултати

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

Код

from collections import defaultdict
def groupby(func,seq):
res = defaultdict(list)
[res[func(x)].append(x) for x in seq]
return res
import functools
def iterate(func):
key = lambda x: x
while True :
yield key
key = functools.partial(lambda f,*args: func(f(*args)), key)
import itertools
def zip_with(func, *iterables):
iterators = list(map(iter,iterables))
return [func(*tuple([next(iterator) for iterator in iterators])) for i in range(min(map(len,list(iterables))))]
from collections import OrderedDict
def cache(func , cache_size):
def func_cached(x , d = OrderedDict()):
if x not in d.keys():
if len(d) >= cache_size :
d.popitem(False)
d[x] = func(x)
return d[x]
return lambda x: func_cached(x)

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

..EE...........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-njs1hs/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

======================================================================
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-njs1hs/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-njs1hs/solution.py", line 31, in <lambda>
    return lambda x: func_cached(x)
  File "/tmp/d20130408-29081-njs1hs/solution.py", line 28, in func_cached
    d.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_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-njs1hs/test.py", line 66, in test_zip_with_empty
    actual = solution.zip_with(lambda x: x)
  File "/tmp/d20130408-29081-njs1hs/solution.py", line 20, in zip_with
    return [func(*tuple([next(iterator) for iterator in iterators])) for i in range(min(map(len,list(iterables))))]
ValueError: min() arg is an empty sequence

======================================================================
ERROR: test_zip_with_infinite_sequence (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-njs1hs/test.py", line 81, in test_zip_with_infinite_sequence
    actual = solution.zip_with(lambda x, y, z: x + y + z, first_names, spaces, last_names)
  File "/tmp/d20130408-29081-njs1hs/solution.py", line 20, in zip_with
    return [func(*tuple([next(iterator) for iterator in iterators])) for i in range(min(map(len,list(iterables))))]
TypeError: object of type 'itertools.repeat' has no len()

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

FAILED (errors=4)

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

Десислава обнови решението на 14.03.2013 22:31 (преди над 11 години)

+from collections import defaultdict
+
+def groupby(func,seq):
+ res = defaultdict(list)
+ [res[func(x)].append(x) for x in seq]
+ return res
+
+import functools
+
+def iterate(func):
+ key = lambda x: x
+ while True :
+ yield key
+ key = functools.partial(lambda f,*args: func(f(*args)), key)
+
+import itertools
+
+def zip_with(func, *iterables):
+ iterators = list(map(iter,iterables))
+ return [func(*tuple([next(iterator) for iterator in iterators])) for i in range(min(map(len,list(iterables))))]
+
+from collections import OrderedDict
+
+def cache(func , cache_size):
+ def func_cached(x , d = OrderedDict()):
+ if x not in d.keys():
+ if len(d) >= cache_size :
+ d.popitem(False)
+ d[x] = func(x)
+ return d[x]
+ return lambda x: func_cached(x)
80417.py:3:1: E302 expected 2 blank lines, found 1
80417.py:3:17: E231 missing whitespace after ','
80417.py:10:1: E302 expected 2 blank lines, found 1
80417.py:12:15: E203 whitespace before ':'
80417.py:14:41: E231 missing whitespace after ','
80417.py:15:1: W293 blank line contains whitespace
80417.py:18:1: E302 expected 2 blank lines, found 1
80417.py:19:30: E231 missing whitespace after ','
80417.py:20:80: E501 line too long (115 > 79 characters)
80417.py:20:96: E231 missing whitespace after ','
80417.py:24:1: E302 expected 2 blank lines, found 1
80417.py:24:15: E203 whitespace before ','
80417.py:25:22: E203 whitespace before ','
80417.py:25:26: E251 unexpected spaces around keyword / parameter equals
80417.py:25:28: E251 unexpected spaces around keyword / parameter equals
80417.py:31:36: E203 whitespace before ':'