Решение на Четири функции от Камелия Пандаклиева

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

Към профила на Камелия Пандаклиева

Резултати

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

Код

import itertools
import collections
def groupby(func, seq):
result = {}
for arg in seq:
key = func(arg)
if key in result:
result[key].append(arg)
else:
result[key] = [arg]
return result
def compose(func, composite_func):
return lambda x: func(composite_func(x))
def iterate(func):
composite_func = lambda x: x
yield composite_func
for i in itertools.count():
composite_func = compose(func, composite_func)
yield composite_func
def zip_with(func, *iterables):
for args in zip(*iterables):
yield func(*args)
def check_cache(func, cached_results, x):
for result in cached_results:
if result[0] == x:
return result[1]
result = func(x)
cached_results.append((x, result))
return result
def cache(func, cache_size):
cached_results = collections.deque(maxlen=cache_size)
return lambda x: check_cache(func, cached_results, x)

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

..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-1348y8o/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

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

FAILED (errors=1)

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

Камелия обнови решението на 14.03.2013 21:47 (преди около 11 години)

+import itertools
+import collections
+
+
+def groupby(func, seq):
+ result = {}
+ for arg in seq:
+ key = func(arg)
+ if result.__contains__(key):
+ result[key].append(arg)
+ else:
+ result[key] = [arg]
+ return result
+
+
+def compose(func, composite_func):
+ return lambda x: func(composite_func(x))
+
+
+def iterate(func):
+ composite_func = lambda x: x
+ yield composite_func
+ for i in itertools.count():
+ composite_func = compose(func, composite_func)
+ yield composite_func
+
+
+def zip_with(func, *iterables):
+ for args in zip(*iterables):
+ yield func(*args)
+
+
+def check_cache(func, cached_results, x):
+ for result in cached_results:
+ if result[0] == x:
+ return result[1]
+ result = func(x)
+ cached_results.append((x, result))
+ return result
+
+
+def cache(func, cache_size):
+ cached_results = collections.deque(maxlen=cache_size)
+ return lambda x: check_cache(func, cached_results, x)

Нямам приличен отговор, освен, че още се уча да мисля на python - просто не се сетих, въпреки че моят вариант ми стои грозновато. И в останалите три функции имаше доста странни неща, надявам се поне повечето да съм ги премахнала - разликата е, че за тях тестовете ми не минаваха в началото, докато тестовете за groupby минаха от първия път и не я мислих много след това.

Камелия обнови решението на 15.03.2013 08:22 (преди около 11 години)

import itertools
import collections
def groupby(func, seq):
result = {}
for arg in seq:
key = func(arg)
- if result.__contains__(key):
+ if key in result:
result[key].append(arg)
else:
result[key] = [arg]
return result
def compose(func, composite_func):
return lambda x: func(composite_func(x))
def iterate(func):
composite_func = lambda x: x
yield composite_func
for i in itertools.count():
composite_func = compose(func, composite_func)
yield composite_func
def zip_with(func, *iterables):
for args in zip(*iterables):
yield func(*args)
def check_cache(func, cached_results, x):
for result in cached_results:
if result[0] == x:
return result[1]
result = func(x)
cached_results.append((x, result))
return result
def cache(func, cache_size):
cached_results = collections.deque(maxlen=cache_size)
return lambda x: check_cache(func, cached_results, x)