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

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

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

Резултати

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

Код

from functools import reduce
def groupby(func, seq):
group = {}
for item in seq:
key = func(item)
group.setdefault(key, []).append(item)
return group
def iterate(func):
counter = 0
while True:
if counter:
compose = reduce(lambda f, g: lambda x: f(g(x)), [func]*counter)
yield compose
else:
yield lambda *args, **kwargs: args if len(args) > 1 else args[0]
counter += 1
def zip_with(func, *iterables):
min_len = None
for iterable in iterables:
iter_len = len(iterable)
if min_len is None or iter_len < min_len:
min_len = iter_len
for i in range(min_len):
yield func(*[iterable[i] for iterable in iterables])
def cache(func, cache_size):
func_cached = {}
items_in_cache = []
def cache_checker(*args, **kwargs):
if args not in func_cached.keys():
result = func(*args, **kwargs)
func_cached[args] = result
items_in_cache.append(args)
if len(items_in_cache) > cache_size:
key_to_delete = items_in_cache[0]
del items_in_cache[0]
del func_cached[key_to_delete]
return result
else:
return func_cached[args]
return cache_checker

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

...............EE...
======================================================================
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-1hfkh8m/test.py", line 67, in test_zip_with_empty
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1hfkh8m/solution.py", line 30, in zip_with
    for i in range(min_len):
TypeError: 'NoneType' object cannot be interpreted as an integer

======================================================================
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-1hfkh8m/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1hfkh8m/solution.py", line 26, in zip_with
    iter_len = len(iterable)
TypeError: object of type 'itertools.repeat' has no len()

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

FAILED (errors=2)

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

Беатрис обнови решението на 15.03.2013 23:39 (преди над 11 години)

+from functools import reduce
+
+
+def groupby(func, seq):
+ group = {}
+ for item in seq:
+ key = func(item)
+ group.setdefault(key, []).append(item)
+ return group
+
+
+def iterate(func):
+ counter = 0
+ while True:
+ if counter:
+ compose = reduce(lambda f, g: lambda x: f(g(x)), [func]*counter)
+ yield compose
+ else:
+ yield lambda *args, **kwargs: args if len(args) > 1 else args[0]
+ counter += 1
+
+
+def zip_with(func, *iterables):
+ min_len = None
+ for iterable in iterables:
+ iter_len = len(iterable)
+ if min_len is None or iter_len < min_len:
+ min_len = iter_len
+
+ for i in range(min_len):
+ yield func(*[iterable[i] for iterable in iterables])
+
+
+def cache(func, cache_size):
+ func_cached = {}
+ items_in_cache = []
+
+ def cache_checker(*args, **kwargs):
+ if args not in func_cached.keys():
+ result = func(*args, **kwargs)
+ func_cached[args] = result
+ items_in_cache.append(args)
+
+ if len(items_in_cache) > cache_size:
+ key_to_delete = items_in_cache[0]
+ del items_in_cache[0]
+ del func_cached[key_to_delete]
+
+ return result
+ else:
+ return func_cached[args]
+
+ return cache_checker