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

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

Към профила на Иван Боршуков

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
result.setdefault(func(element), []).append(element)
return result
def zip_with(func, *iterables):
return map(func, *iterables) if iterables else []
def cache(func, cache_size=0):
caches = OrderedDict()
def func_cached(*args):
if args in caches:
pass
else:
if len(caches) == cache_size:
caches.popitem(False)
caches[args] = func(*args)
return caches[args]
return func_cached
def compose(f, g):
return lambda *args, **kwargs: f(g(*args, **kwargs))
def iterate(func):
composition = lambda x: x
while True:
yield composition
composition = compose(func, composition)

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

...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-14bvems/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-14bvems/solution.py", line 23, in func_cached
    caches.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 версии и 3 коментара)

Иван обнови решението на 13.03.2013 15:10 (преди около 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ res = {}
+ for elem in seq:
+ res.setdefault(func(elem), []).append(elem) # setdefaul returns list
+ return res
+
+
+def zip_with(func, *iterables):
+ return map(func, *iterables) if iterables else []
+
+
+def cache(func, cache_size=0):
+ caches = OrderedDict()
+
+ def func_cached(*args):
+ if args in caches:
+ pass # the value is present, do nothing
+ else:
+ if len(caches) == cache_size:
+ caches.popitem(False) # FIFO
+ caches[args] = func(*args)
+ return caches[args]
+ return func_cached
+
+
+def compose(f, g):
+ return lambda *args, **kwargs: f(g(*args, **kwargs))
+
+
+def iterate(func):
+ composition = lambda x: x
+ while True:
+ yield composition
+ composition = compose(func, composition) # f(id), f(f(id))

Има някакви общоприети съкращения. func е едно от тях, поради някаква причина. Други популярни са args и kwargs. Такива е ок да ползваш.

Но като цяло избягвай всякакви съкращения. Ако си мислиш за резултат, напиши result, не res. Същото и за елемент...

Иван обнови решението на 15.03.2013 17:23 (преди около 11 години)

from collections import OrderedDict
def groupby(func, seq):
- res = {}
- for elem in seq:
- res.setdefault(func(elem), []).append(elem) # setdefaul returns list
- return res
+ result = {}
+ for element in seq:
+ result.setdefault(func(element), []).append(element)
+ return result
def zip_with(func, *iterables):
return map(func, *iterables) if iterables else []
def cache(func, cache_size=0):
caches = OrderedDict()
def func_cached(*args):
if args in caches:
- pass # the value is present, do nothing
+ pass
else:
if len(caches) == cache_size:
- caches.popitem(False) # FIFO
+ caches.popitem(False)
caches[args] = func(*args)
return caches[args]
return func_cached
def compose(f, g):
return lambda *args, **kwargs: f(g(*args, **kwargs))
def iterate(func):
composition = lambda x: x
while True:
yield composition
- composition = compose(func, composition) # f(id), f(f(id))
+ composition = compose(func, composition)