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

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

Към профила на Марио Даскалов

Резултати

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

Код

from collections import deque
import itertools
def groupby(func, seq):
function_map = dict()
for i in seq:
res = func(i)
if res not in function_map:
function_map[res] = []
function_map[res].append(i)
return function_map
def iterate(func):
def identity(*args):
if len(args) == 1:
return args[0]
else:
return args
for i in itertools.count():
def composed_func(*args):
if i == 0:
return identity(*args)
else:
res = args
for x in range(i):
if x == 0:
res = func(*args)
else:
res = func(res)
return res
yield composed_func
def zip_with(func, *iterables):
size = -1
for iterable in iterables:
iterable_size = len(iterable)
if iterable_size < size or size == -1:
size = iterable_size
for i in range(size):
list = []
for iterable in iterables:
list.append(iterable[i])
yield func(*list)
def cache(func, cache_size):
values_dict = dict()
values_order = deque()
def cached_func(*args):
if args not in values_dict:
if len(values_order) >= cache_size:
last_element = values_order.pop()
del values_dict[last_element]
values_order.appendleft(args)
values_dict[args] = func(*args)
return values_dict[args]
return cached_func

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

...E.......FFF..E...
======================================================================
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-tx9p32/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-tx9p32/solution.py", line 58, in cached_func
    last_element = values_order.pop()
IndexError: pop from an empty deque

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

======================================================================
FAIL: test_iterate_out_of_order_calls (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-tx9p32/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: test_iterate_out_of_order_calls_again (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-tx9p32/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: test_iterate_out_of_order_calls_yet_again (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-tx9p32/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


----------------------------------------------------------------------
Ran 20 tests in 0.018s

FAILED (failures=3, errors=2)

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

Марио обнови решението на 15.03.2013 22:15 (преди над 11 години)

+from collections import deque
+import itertools
+
+
+def groupby(func, seq):
+ function_map = dict()
+ for i in seq:
+ res = func(i)
+ if res not in function_map:
+ function_map[res] = []
+ function_map[res].append(i)
+ return function_map
+
+
+def iterate(func):
+ def identity(*args):
+ if len(args) == 1:
+ return args[0]
+ else:
+ return args
+
+ for i in itertools.count():
+ def composed_func(*args):
+ if i == 0:
+ return identity(*args)
+ else:
+ res = args
+ for x in range(i):
+ if x == 0:
+ res = func(*args)
+ else:
+ res = func(res)
+ return res
+ yield composed_func
+
+
+def zip_with(func, *iterables):
+ size = -1
+ for iterable in iterables:
+ iterable_size = len(iterable)
+ if iterable_size < size or size == -1:
+ size = iterable_size
+
+ for i in range(size):
+ list = []
+ for iterable in iterables:
+ list.append(iterable[i])
+ yield func(*list)
+
+
+def cache(func, cache_size):
+ values_dict = dict()
+ values_order = deque()
+
+ def cached_func(*args):
+ if args not in values_dict:
+ if len(values_order) >= cache_size:
+ last_element = values_order.pop()
+ del values_dict[last_element]
+ values_order.appendleft(args)
+ values_dict[args] = func(*args)
+ return values_dict[args]
+
+ return cached_func