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

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

Към профила на Радослав Върбанов

Резултати

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

Код

import itertools
from collections import OrderedDict
def groupby(criteria, sequence):
groups = {}
for elem in sequence:
i = criteria(elem)
if i in groups.keys():
groups[i].append(elem)
else:
groups[i] = [elem]
return groups
def iterate(func):
element = lambda x: x
for i in itertools.count():
yield element
element = compose(func, element)
def compose(func1,func2):
return lambda *args: func1(func2(*args))
def zip_with(func, *args):
if(len(args) == 0):
return iter([])
return iter(map(func, *args))
CACHE = OrderedDict()
def cache(func, levels):
def func_cached(*args):
if args not in CACHE:
if len(CACHE) >= levels:
CACHE.popitem(False)
CACHE[args] = func(*args)
return CACHE[args]
return func_cached

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

F..FF...............
======================================================================
FAIL: test_cache_cache_is_not_global (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-1tsi3gl/test.py", line 133, in test_cache_cache_is_not_global
    self.assertEqual(2, call_count)
AssertionError: 2 != 1

======================================================================
FAIL: 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-1tsi3gl/test.py", line 106, in test_cache_no_cache
    self.assertEqual(2, call_count)
AssertionError: 2 != 1

======================================================================
FAIL: test_cache_size_is_respected (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-1tsi3gl/test.py", line 149, in test_cache_size_is_respected
    self.assertEqual(4, call_count)
AssertionError: 4 != 3

----------------------------------------------------------------------
Ran 20 tests in 0.015s

FAILED (failures=3)

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

Радослав обнови решението на 15.03.2013 23:50 (преди над 11 години)

+import itertools
+from collections import OrderedDict
+
+
+def groupby(criteria, sequence):
+ groups = {}
+ for elem in sequence:
+ i = criteria(elem)
+ if i in groups.keys():
+ groups[i].append(elem)
+ else:
+ groups[i] = [elem]
+ return groups
+
+
+def identity(x):
+ return x
+
+
+def iterate(func):
+ element = identity
+ for i in itertools.count():
+ yield element
+ element = compose(func, element)
+
+
+def compose(func1,func2):
+ return lambda *args: func1(func2(*args))
+
+
+def zip_with(func, *args):
+ if(len(args) == 0):
+ return iter([])
+ return iter(map(func, *args))
+
+
+CACHE = OrderedDict()
+
+
+def cache(func, levels):
+
+ def func_cached(*args):
+ if args not in CACHE:
+ if len(CACHE) >= levels:
+ CACHE.popitem(last=False)
+ CACHE[args] = func(*args)
+ return CACHE[args]
+ return func_cached

Радослав обнови решението на 15.03.2013 23:58 (преди над 11 години)

import itertools
from collections import OrderedDict
def groupby(criteria, sequence):
groups = {}
for elem in sequence:
i = criteria(elem)
if i in groups.keys():
groups[i].append(elem)
else:
groups[i] = [elem]
return groups
-def identity(x):
- return x
-
-
def iterate(func):
- element = identity
+ element = lambda x: x
for i in itertools.count():
yield element
element = compose(func, element)
def compose(func1,func2):
return lambda *args: func1(func2(*args))
def zip_with(func, *args):
if(len(args) == 0):
return iter([])
return iter(map(func, *args))
CACHE = OrderedDict()
def cache(func, levels):
def func_cached(*args):
if args not in CACHE:
if len(CACHE) >= levels:
- CACHE.popitem(last=False)
+ CACHE.popitem(False)
CACHE[args] = func(*args)
return CACHE[args]
return func_cached