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

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

Към профила на Орлин Христов

Резултати

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

Код

from collections import OrderedDict
from itertools import count
def cache(func, cache_size):
cached_arguments = OrderedDict()
def func_cached(*args):
if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
composer = lambda n: lambda x: func(composer(n-1)(x)) if n > 0 else x
return (composer(n) for n in count())
def groupby(func, seq):
groups = {}
for argument in seq:
groups.setdefault(func(argument), []).append(argument)
return groups

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

...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-1akzcon/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1akzcon/solution.py", line 11, in func_cached
    cached_arguments.popitem(last=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)

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

Орлин обнови решението на 12.03.2013 15:31 (преди над 11 години)

+from collections import OrderedDict, defaultdict
+
+
+def cache(func, cache_size):
+ cache = OrederedDict()
+
+ def func_cached(*args):
+ if args not in cach_vault:
+ if len(cache) == cach_size:
+ cache.popitem(last=False)
+ cache[args] = func(*args)
+ return cache[args]
+
+ return func_cached
+
+def zip_with(func, *iterables):
+ return (func(*args) for args in zip(*iterables)
+
+def iterate(func):
+ current_function = lambda x: x
+ while True:
+ yield current_function
+ current_function = lambda x: func(current_function(x))
+
+def group_by(func, seq):
+ groups = {}
+ for argument in seq:
+ functional = func(argument)
+ if functional not in groups:
+ groups[functional] = []
+ groups[functional].append(argument)
+ return groups

Орлин обнови решението на 12.03.2013 15:32 (преди над 11 години)

from collections import OrderedDict, defaultdict
def cache(func, cache_size):
cache = OrederedDict()
def func_cached(*args):
if args not in cach_vault:
if len(cache) == cach_size:
cache.popitem(last=False)
cache[args] = func(*args)
return cache[args]
return func_cached
def zip_with(func, *iterables):
- return (func(*args) for args in zip(*iterables)
+ return (func(*args) for args in zip(*iterables))
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = lambda x: func(current_function(x))
def group_by(func, seq):
groups = {}
for argument in seq:
functional = func(argument)
if functional not in groups:
groups[functional] = []
groups[functional].append(argument)
return groups

Орлин обнови решението на 12.03.2013 22:03 (преди над 11 години)

from collections import OrderedDict, defaultdict
def cache(func, cache_size):
- cache = OrederedDict()
+ cached_arguments = OrederedDict()
def func_cached(*args):
- if args not in cach_vault:
- if len(cache) == cach_size:
- cache.popitem(last=False)
- cache[args] = func(*args)
- return cache[args]
+ if args not in cached_arguments:
+ if len(cached_arguments) == cache_size:
+ cached_arguments.popitem(last=False)
+ cached_arguments[args] = func(*args)
+ return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = lambda x: func(current_function(x))
def group_by(func, seq):
groups = {}
for argument in seq:
functional = func(argument)
if functional not in groups:
groups[functional] = []
groups[functional].append(argument)
return groups

Орлин обнови решението на 13.03.2013 09:02 (преди над 11 години)

from collections import OrderedDict, defaultdict
def cache(func, cache_size):
cached_arguments = OrederedDict()
def func_cached(*args):
if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = lambda x: func(current_function(x))
def group_by(func, seq):
groups = {}
for argument in seq:
- functional = func(argument)
- if functional not in groups:
- groups[functional] = []
- groups[functional].append(argument)
+ groups.setdefault(func(argument), []).append(argument)
return groups

Орлин обнови решението на 13.03.2013 09:36 (преди над 11 години)

from collections import OrderedDict, defaultdict
def cache(func, cache_size):
cached_arguments = OrederedDict()
def func_cached(*args):
if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
+
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
+
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = lambda x: func(current_function(x))
+
def group_by(func, seq):
groups = {}
for argument in seq:
groups.setdefault(func(argument), []).append(argument)
return groups

Орлин обнови решението на 13.03.2013 09:46 (преди над 11 години)

from collections import OrderedDict, defaultdict
def cache(func, cache_size):
cached_arguments = OrederedDict()
def func_cached(*args):
- if args not in cached_arguments:
+ if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
current_function = lambda x: x
while True:
yield current_function
current_function = lambda x: func(current_function(x))
def group_by(func, seq):
groups = {}
for argument in seq:
groups.setdefault(func(argument), []).append(argument)
return groups

Орлин обнови решението на 14.03.2013 17:50 (преди над 11 години)

-from collections import OrderedDict, defaultdict
+from collections import OrderedDict
+from itertools import count
def cache(func, cache_size):
cached_arguments = OrederedDict()
def func_cached(*args):
if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
- current_function = lambda x: x
- while True:
- yield current_function
- current_function = lambda x: func(current_function(x))
+ composer = lambda n: lambda x: func(composer(n-1)(x)) if n > 0 else x
+ return (composer(n) for n in count())
def group_by(func, seq):
groups = {}
for argument in seq:
groups.setdefault(func(argument), []).append(argument)
return groups

Орлин обнови решението на 14.03.2013 18:18 (преди над 11 години)

from collections import OrderedDict
from itertools import count
def cache(func, cache_size):
- cached_arguments = OrederedDict()
+ cached_arguments = OrderedDict()
def func_cached(*args):
if args not in cached_arguments:
if len(cached_arguments) == cache_size:
cached_arguments.popitem(last=False)
cached_arguments[args] = func(*args)
return cached_arguments[args]
return func_cached
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def iterate(func):
composer = lambda n: lambda x: func(composer(n-1)(x)) if n > 0 else x
return (composer(n) for n in count())
-def group_by(func, seq):
+def groupby(func, seq):
groups = {}
for argument in seq:
groups.setdefault(func(argument), []).append(argument)
- return groups
+ return groups