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

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

Към профила на Виктория Христова

Резултати

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

Код

import collections
def groupby(func, seq):
groups = {}
for element in seq:
groups.setdefault(func(element), []).append(element)
return groups
def compose(f, g):
return lambda x: f(g(x))
def iterate(func):
composition = lambda x: x
while True:
yield composition
composition = compose(func, composition)
def zip_with(func, *iterables):
return (func(*x) for x in zip(*iterables))
def cache(func, cache_size):
func_copy = lambda *x: func(*x)
func_copy.cache = collections.OrderedDict()
def func_cached(*x):
if not x in func_copy.cache.keys():
if len(func_copy.cache) == cache_size:
func_copy.cache.popitem(False)
func_copy.cache[x] = func_copy(*x)
return func_copy.cache[x]
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-ht5wfy/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-ht5wfy/solution.py", line 34, in func_cached
    func_copy.cache.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)

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

Виктория обнови решението на 14.03.2013 22:46 (преди над 11 години)

+import collections
+
+
+def identity(x):
+ return x
+
+
+def compose(f, g):
+ return lambda x: f(g(x))
+
+
+def iterate(func):
+ composition = identity
+
+ while True:
+ yield composition
+ composition = compose(func, composition)
+
+
+def groupby(func, seq):
+ groups = {}
+ for element in seq:
+ groups.setdefault(func(element), []).append(element)
+ return groups
+
+
+def zip_with(func, *iterables):
+ return (func(*x) for x in zip(*iterables))
+
+
+def cache(func, cache_size):
+ func_copy = func
+ func_copy.cache = collections.OrderedDict()
+
+ def func_cached(x):
+ if not x in func_copy.cache.keys():
+ if len(func_copy.cache) == cache_size:
+ func_copy.cache.popitem(False)
+ func_copy.cache[x] = func_copy(x)
+ return func_copy.cache[x]
+
+ return func_cached

Виктория обнови решението на 15.03.2013 10:22 (преди над 11 години)

import collections
-def identity(x):
- return x
+def groupby(func, seq):
+ groups = {}
+ for element in seq:
+ groups.setdefault(func(element), []).append(element)
+ return groups
def compose(f, g):
return lambda x: f(g(x))
def iterate(func):
- composition = identity
+ composition = lambda x: x
while True:
yield composition
composition = compose(func, composition)
-def groupby(func, seq):
- groups = {}
- for element in seq:
- groups.setdefault(func(element), []).append(element)
- return groups
-
-
def zip_with(func, *iterables):
return (func(*x) for x in zip(*iterables))
def cache(func, cache_size):
- func_copy = func
+ func_copy = lambda *x: func(*x)
func_copy.cache = collections.OrderedDict()
- def func_cached(x):
+ def func_cached(*x):
if not x in func_copy.cache.keys():
if len(func_copy.cache) == cache_size:
func_copy.cache.popitem(False)
- func_copy.cache[x] = func_copy(x)
+ func_copy.cache[x] = func_copy(*x)
return func_copy.cache[x]
return func_cached