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

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

Към профила на Цанислава Русева

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
dictionary = {}
for element in seq:
dictionary.setdefault(func(element), []).append(element)
return OrderedDict(sorted(dictionary.items()))
def compose(func1, func2):
def result(x):
return func1(func2(x))
return result
def iterate(func):
result = lambda x: x
yield result
while True:
result = compose(func, result)
yield result
def zip_with(func, *iterables):
for iterables in zip(*iterables):
yield func(*iterables)
def cache(func, cache_size):
if cache_size == 0:
return func
cache = [(None, None)]*cache_size
def func_cached(argument):
for cache_element in cache:
if cache_element[0] == argument:
return cache_element[1]
cache.pop(0)
cache.append((argument, func(argument)))
return cache[cache_size-1][1]
return func_cached

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

..E...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-1ipst44/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_groupby_no_repetitions (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-1ipst44/test.py", line 14, in test_groupby_no_repetitions
    actual = solution.groupby(lambda x: x, [1, 2, 3, 5,'se7en'], )
  File "/tmp/d20130408-29081-1ipst44/solution.py", line 8, in groupby
    return OrderedDict(sorted(dictionary.items()))
TypeError: unorderable types: str() < int()

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

FAILED (errors=2)

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

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

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ dictionary = {}
+ for element in seq:
+ dictionary.setdefault(func(element), []).append(element)
+ return OrderedDict(sorted(dictionary.items()))
+
+def compose(func1, func2):
+ def result(x):
+ return func1(func2(x))
+ return result
+
+def iterate(func):
+ result = lambda x: x
+ yield result
+ while True:
+ result = compose(func, result)
+ yield result
+
+def zip_with(func, *iterables):
+ for iterables in zip(*iterables):
+ yield func(*iterables)
+
+def cache(func, cache_size):
+ if cache_size == 0:
+ return func
+ cache = [(None, None)]*cache_size
+ def func_cached(argument):
+ for cache_element in cache:
+ if cache_element[0] == argument:
+ return cache_element[1]
+ cache.pop(0)
+ cache.append((argument, func(argument)))
+ return cache[cache_size-1][1]
+ return func_cached
31212.py:7:65: W291 trailing whitespace
31212.py:10:1: E302 expected 2 blank lines, found 1
31212.py:14:1: W293 blank line contains whitespace
31212.py:15:1: E302 expected 2 blank lines, found 1
31212.py:21:1: W293 blank line contains whitespace
31212.py:22:1: E302 expected 2 blank lines, found 1
31212.py:25:1: W293 blank line contains whitespace
31212.py:26:1: E302 expected 2 blank lines, found 1
31212.py:30:5: E301 expected 1 blank line, found 0