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

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

Към профила на Ралица Маркова

Резултати

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

Код

def groupby(func, seq):
dictionary = {}
items = [(func(x), x) for x in seq]
for key, value in items:
if key not in dictionary.keys():
dictionary[key] = [value]
else:
dictionary[key].append(value)
return dictionary
def composition(func1, func2):
return lambda x: func1(func2(x))
def iterate(func):
composition_func = lambda x: x
while True:
yield composition_func
composition_func = composition(composition_func, func)
def zip_with(func, *iterables):
for i in range(0, len(iterables)):
args = []
for iterable in iterables:
if i == len(iterable):
return
args.append(iterable[i])
yield func(*args)
i += 1
def cache(func, cache_size):
cached_items = []
def func_cached(x):
result = [func_result for (arg, func_result) in cached_items
if arg == x]
if [] != result:
return result[0]
result = func(x)
if len(cached_items) == cache_size:
cached_items.remove(cached_items[0])
cached_items.append((x, result))
return result
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-71ga8e/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-71ga8e/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-71ga8e/solution.py", line 44, in func_cached
    cached_items.remove(cached_items[0])
IndexError: list index out of range

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

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

FAILED (errors=3)

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

Ралица обнови решението на 15.03.2013 20:24 (преди над 11 години)

+def groupby(func, seq):
+ dictionary = {}
+ items = [(func(x), x) for x in seq]
+ for key, value in items:
+ if key not in dictionary.keys():
+ dictionary[key] = [value]
+ else:
+ dictionary[key].append(value)
+ return dictionary
+
+
+def composition(func1, func2):
+ return lambda x: func1(func2(x))
+
+
+def iterate(func):
+ composition_func = lambda x: x
+ while True:
+ yield composition_func
+ composition_func = composition(composition_func, func)
+
+
+def zip_with(func, *iterables):
+ for i in range(0, len(iterables)):
+ args = []
+ for iterable in iterables:
+ if i == len(iterable):
+ return
+ args.append(iterable[i])
+ yield func(*args)
+ i += 1
+
+
+def cache(func, cache_size):
+ cached_items = []
+
+ def func_cached(x):
+ result = [func_result for (arg, func_result) in cached_items
+ if arg == x]
+ if [] != result:
+ return result[0]
+ result = func(x)
+ if len(cached_items) == cache_size:
+ cached_items.remove(cached_items[0])
+ cached_items.append((x, result))
+ return result
+ return func_cached