Решение на Четири функции от Филарета Йорданова

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

Към профила на Филарета Йорданова

Резултати

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

Код

from collections import defaultdict
from collections import OrderedDict
def groupby(func, seq):
keys = map(func, seq)
group_values = defaultdict(list)
iterator = iter(keys)
for item in seq:
group_values[next(iterator)].append(item)
return group_values
def composer(f, g):
return lambda x: f(g(x))
def iterate(func):
yield lambda x: x
composition = func
while True:
yield composition
composition = composer(func, composition)
def length(iterable):
for index, item in enumerate(iterable):
length_ = index + 1
return length_
def zip_with(func, *iterables):
if not all([item is None for item in iterables]):
min_length = min(map(length, iterables))
for i in range(min_length):
yield func(*(map(lambda k: k[i], iterables)))
def cache(func, cache_size):
def func_cached(*args):
if not hasattr(func, '_cache'):
func._cache = OrderedDict()
if len(func._cache) > cache_size:
func._cache.popitem(False)
if not args in func._cache:
func._cache[args] = func(*args)
return func._cache[args]
return func_cached

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

F...............E...
======================================================================
ERROR: test_zip_with_infinite_sequence (test.SecondHomeworkTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
FAIL: 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-96t8ef/test.py", line 133, in test_cache_cache_is_not_global
    self.assertEqual(2, call_count)
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 20 tests in 2.078s

FAILED (failures=1, errors=1)

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

Филарета обнови решението на 14.03.2013 23:14 (преди около 11 години)

+from collections import defaultdict
+
+
+def groupby(func, seq):
+ keys = map(func, seq)
+ group_values = defaultdict(list)
+ iterator = iter(keys)
+ for item in seq:
+ group_values[next(iterator)].append(item)
+ return group_values
+
+
+def composer(f, g):
+ return lambda x: f(g(x))
+
+
+def iterate(func):
+ yield lambda x: x
+ composition = func
+ while True:
+ yield composition
+ composition = composer(func, composition)
+
+
+def zip_with(func, *iterables):
+ if not all([item is None for item in iterables]):
+ min_length = min(map(len, iterables))
+ for i in range(min_length):
+ yield func(*(map(lambda k: k[i], iterables)))
+
+
+def cache(func, cache_size):
+ def func_cached(*args):
+ if not hasattr(func, '_cache'):
+ func._cache = {}
+ if args in func._cache:
+ return func._cache[args]
+ new_value = func(*args)
+ func._cache[args] = new_value
+ return new_value
+ return func_cached

Филарета обнови решението на 15.03.2013 14:12 (преди около 11 години)

from collections import defaultdict
+from collections import OrderedDict
def groupby(func, seq):
keys = map(func, seq)
group_values = defaultdict(list)
iterator = iter(keys)
for item in seq:
group_values[next(iterator)].append(item)
return group_values
def composer(f, g):
return lambda x: f(g(x))
def iterate(func):
yield lambda x: x
composition = func
while True:
yield composition
composition = composer(func, composition)
+def length(iterable):
+ for index, item in enumerate(iterable):
+ length_ = index + 1
+ return length_
+
+
def zip_with(func, *iterables):
if not all([item is None for item in iterables]):
- min_length = min(map(len, iterables))
+ min_length = min(map(length, iterables))
for i in range(min_length):
yield func(*(map(lambda k: k[i], iterables)))
def cache(func, cache_size):
def func_cached(*args):
if not hasattr(func, '_cache'):
- func._cache = {}
- if args in func._cache:
- return func._cache[args]
- new_value = func(*args)
- func._cache[args] = new_value
- return new_value
+ func._cache = OrderedDict()
+ if len(func._cache) > cache_size:
+ func._cache.popitem(False)
+ if not args in func._cache:
+ func._cache[args] = func(*args)
+ return func._cache[args]
return func_cached