Решение на Четири функции от Александър Петков

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

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

Резултати

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

Код

from collections import OrderedDict
def groupby(func, sec):
result = {}
for i in sec:
if func(i) in result:
result[func(i)].append(i)
else:
result[func(i)] = [i]
return result
def composition(func1, func2):
return lambda *args: func1(func2(*args))
def iterate(func):
composing = lambda x: x
yield composing
while True:
yield composition(func, composing)
composing = composition(func, composing)
def zip_with(func, *iterables):
if not len(list(iterables)):
return iter([])
return map(lambda *args: func(*args), *iterables)
def cache(func, cache_size):
storage = OrderedDict({})
def func_cached(*args):
if args in storage:
return storage[args]
if len(storage) >= cache_size:
storage.popitem(False)
storage[args] = func(*args)
return storage[args]
return func_cached

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

...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-1hm6dxo/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1hm6dxo/solution.py", line 39, in func_cached
    storage.popitem(False)
  File "/opt/python3.3/lib/python3.3/collections/__init__.py", line 114, in popitem
    raise KeyError('dictionary is empty')
KeyError: 'dictionary is empty'

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

FAILED (errors=1)

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

Александър обнови решението на 15.03.2013 00:43 (преди над 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, sec):
+ result = {}
+ for i in sec:
+ if func(i) in result:
+ result[func(i)].append(i)
+ else:
+ result[func(i)] = [i]
+ return result
+
+
+def composition(func1, func2):
+ return lambda *args: func1(func2(*args))
+
+
+def iterate(func):
+ composing = lambda x: x
+ yield composing
+ while True:
+ yield composition(func, composing)
+ composing = composition(func, composing)
+
+
+def zip_with(func, *iterables):
+ if not len(list(iterables)):
+ return iter([])
+ return map(lambda *args: func(*args), *iterables)
+
+
+def cache(func, cache_size):
+ storage = {}
+
+ def func_cached(*args):
+ if args in storage:
+ return storage[args]
+ if len(storage) >= cache_size:
+ storage.popitem(False)
+ storage[args] = func(*args)
+ return storage[args]
+
+ return func_cached

Александър обнови решението на 15.03.2013 00:46 (преди над 11 години)

from collections import OrderedDict
def groupby(func, sec):
result = {}
for i in sec:
if func(i) in result:
result[func(i)].append(i)
else:
result[func(i)] = [i]
return result
def composition(func1, func2):
return lambda *args: func1(func2(*args))
def iterate(func):
composing = lambda x: x
yield composing
while True:
yield composition(func, composing)
composing = composition(func, composing)
def zip_with(func, *iterables):
if not len(list(iterables)):
return iter([])
return map(lambda *args: func(*args), *iterables)
def cache(func, cache_size):
storage = {}
def func_cached(*args):
if args in storage:
return storage[args]
- if len(storage) >= cache_size:
+ if len(storage) = cache_size:
storage.popitem(False)
storage[args] = func(*args)
return storage[args]
return func_cached

Александър обнови решението на 15.03.2013 15:34 (преди над 11 години)

from collections import OrderedDict
def groupby(func, sec):
result = {}
for i in sec:
if func(i) in result:
result[func(i)].append(i)
else:
result[func(i)] = [i]
return result
def composition(func1, func2):
return lambda *args: func1(func2(*args))
def iterate(func):
composing = lambda x: x
yield composing
while True:
yield composition(func, composing)
composing = composition(func, composing)
def zip_with(func, *iterables):
if not len(list(iterables)):
return iter([])
return map(lambda *args: func(*args), *iterables)
def cache(func, cache_size):
- storage = {}
+ storage = OrderedDict({})
def func_cached(*args):
if args in storage:
return storage[args]
- if len(storage) = cache_size:
+ if len(storage) >= cache_size:
storage.popitem(False)
storage[args] = func(*args)
return storage[args]
return func_cached