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

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

Към профила на Николай Манолов

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
def compose(func1, func2):
return lambda x: func1(func2(x))
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = compose(func, current_function)
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def cached_function(*args):
if args not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
store[args] = func(*args)
return store[args]
return cached_function

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

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

----------------------------------------------------------------------
Ran 20 tests in 0.016s

FAILED (failures=1)

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

Николай обнови решението на 15.03.2013 16:13 (преди над 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ result = {}
+ for element in seq:
+ key = func(element)
+ if key not in result.keys():
+ result[key] = [element]
+ else:
+ result[key].append(element)
+ return result
+
+
+def iterate(func):
+ pass
+
+
+def zip_with(func, *iterables):
+ return (func(*args) for args in zip(*iterables))
+
+
+def cache(func, cache_size):
+ store = OrderedDict()
+
+ def cached_function(x):
+ if x not in store:
+ if len(store) - 1 > cache_size:
+ store.popitem(last=False)
+ store[x] = func(x)
+ return store[x]
+ return cached_function

Николай обнови решението на 15.03.2013 16:16 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
def iterate(func):
pass
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def cached_function(x):
if x not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
store[x] = func(x)
return store[x]
return cached_function
+

Николай обнови решението на 15.03.2013 16:19 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
def iterate(func):
pass
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def cached_function(x):
if x not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
store[x] = func(x)
return store[x]
return cached_function
-

Николай обнови решението на 15.03.2013 20:34 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
def iterate(func):
pass
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def cached_function(x):
if x not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
store[x] = func(x)
return store[x]
return cached_function
+

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

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
def iterate(func):
pass
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
- def cached_function(x):
- if x not in store:
+ def cached_function(*args):
+ if args not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
- store[x] = func(x)
- return store[x]
+ store[args] = func(*args)
+ return store[args]
return cached_function

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

from collections import OrderedDict
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if key not in result.keys():
result[key] = [element]
else:
result[key].append(element)
return result
+def compose(func1, func2):
+ return lambda x: func1(func2(x))
+
+
def iterate(func):
- pass
+ current_function = lambda x: x
+ while True:
+ yield current_function
+ current_function = compose(func, current_function)
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def cached_function(*args):
if args not in store:
if len(store) - 1 > cache_size:
store.popitem(last=False)
store[args] = func(*args)
return store[args]
return cached_function