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

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

Към профила на Мартин Костов

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
answer = {}
for iterable in seq:
answer.setdefault(func(iterable), []).append(iterable)
return answer
def iterate(func):
def compose(func1, func2):
def inner(arg):
return func1(func2(arg))
return inner
count = 0
outer_function = lambda x: x
while True:
if count == 0:
yield lambda x: x
else:
yield compose(func, outer_function)
outer_function = compose(func, outer_function)
count += 1
def zip_with(func, *iterables):
if not iterables:
return
for index in range(len(min(*iterables, key=lambda x: len(x)))):
arguments = []
for iterable in iterables:
arguments.append(iterable[index])
yield func(*arguments)
def cache(func, cache_size):
cached = OrderedDict()
def func_cached(key):
if key in cached:
return cached[key]
else:
if len(cached) >= cache_size:
cached.popitem(last=False)
cached[key] = func(key)
return cached[key]
return func_cached

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

..EE............E...
======================================================================
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-9znp64/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cached() 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-9znp64/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-9znp64/solution.py", line 47, in func_cached
    cached.popitem(last=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_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-9znp64/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-9znp64/solution.py", line 32, in zip_with
    for index in range(len(min(*iterables, key=lambda x: len(x)))):
  File "/tmp/d20130408-29081-9znp64/solution.py", line 32, in <lambda>
    for index in range(len(min(*iterables, key=lambda x: len(x)))):
TypeError: object of type 'itertools.repeat' has no len()

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

FAILED (errors=3)

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

Мартин обнови решението на 13.03.2013 11:29 (преди над 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ answer = {}
+ for i in seq:
+ answer.setdefault(func(i), []).append(i)
+ return answer
+
+
+def iterate(func):
+
+ def compose(fun1, fun2):
+ def inner(x):
+ return fun1(fun2(x))
+ return inner
+
+ count = 0
+ outer_function = lambda x: x
+ while True:
+ if count == 0:
+ yield lambda x: x
+ else:
+ yield compose(func, outer_function)
+ outer_function = compose(func, outer_function)
+ count += 1
+
+
+def zip_with(func, *iterables):
+ for index in range(len(min(*iterables, key=lambda x: len(x)))):
+ arguments = []
+ for iterable in iterables:
+ arguments.append(iterable[index])
+ yield func(*arguments)
+
+
+def cache(func, cache_size):
+ d = OrderedDict()
+
+ def func_cached(x):
+ if x in d:
+ return d[x]
+ else:
+ if len(d) >= cache_size:
+ d.popitem(last=False)
+ d[x] = func(x)
+ return d[x]
+ return func_cached

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

from collections import OrderedDict
def groupby(func, seq):
answer = {}
for i in seq:
answer.setdefault(func(i), []).append(i)
return answer
def iterate(func):
def compose(fun1, fun2):
def inner(x):
return fun1(fun2(x))
return inner
count = 0
outer_function = lambda x: x
while True:
if count == 0:
yield lambda x: x
else:
yield compose(func, outer_function)
outer_function = compose(func, outer_function)
count += 1
def zip_with(func, *iterables):
+ if not iterables:
+ return
for index in range(len(min(*iterables, key=lambda x: len(x)))):
arguments = []
for iterable in iterables:
arguments.append(iterable[index])
yield func(*arguments)
def cache(func, cache_size):
d = OrderedDict()
def func_cached(x):
if x in d:
return d[x]
else:
if len(d) >= cache_size:
d.popitem(last=False)
d[x] = func(x)
return d[x]
return func_cached

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

from collections import OrderedDict
def groupby(func, seq):
answer = {}
- for i in seq:
- answer.setdefault(func(i), []).append(i)
+ for iterable in seq:
+ answer.setdefault(func(iterable), []).append(iterable)
return answer
def iterate(func):
- def compose(fun1, fun2):
- def inner(x):
- return fun1(fun2(x))
+ def compose(func1, func2):
+ def inner(arg):
+ return func1(func2(arg))
return inner
count = 0
outer_function = lambda x: x
while True:
if count == 0:
yield lambda x: x
else:
yield compose(func, outer_function)
outer_function = compose(func, outer_function)
count += 1
def zip_with(func, *iterables):
if not iterables:
return
for index in range(len(min(*iterables, key=lambda x: len(x)))):
arguments = []
for iterable in iterables:
arguments.append(iterable[index])
yield func(*arguments)
def cache(func, cache_size):
- d = OrderedDict()
+ cached = OrderedDict()
- def func_cached(x):
- if x in d:
- return d[x]
+ def func_cached(key):
+ if key in cached:
+ return cached[key]
else:
- if len(d) >= cache_size:
- d.popitem(last=False)
- d[x] = func(x)
- return d[x]
+ if len(cached) >= cache_size:
+ cached.popitem(last=False)
+ cached[key] = func(key)
+ return cached[key]
return func_cached