Решение на Четири функции от Георги Шопов

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

Към профила на Георги Шопов

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args,
**kwargs))
returned_func = lambda arg: arg
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
_cache = OrderedDict()
def func_cached(*args):
if args not in _cache:
if len(_cache) == cache_size:
_cache.popitem(False)
_cache[args] = func(*args)
return _cache[args]
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-1n0a7f1/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1n0a7f1/solution.py", line 38, in func_cached
    _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)

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

Георги обнови решението на 14.03.2013 22:36 (преди над 11 години)

+from functools import lru_cache
+
+
+def groupby(func, seq):
+
+ grouping_result = {}
+
+ for value in seq:
+ func_result = func(value)
+
+ if func_result in grouping_result:
+ grouping_result[func_result] += [value]
+ else:
+ grouping_result[func_result] = [value]
+
+ return grouping_result
+
+
+def iterate(func):
+
+ def compose(outter_func, inner_func):
+ return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
+
+ returned_func = lambda x: x
+
+ while True:
+ yield returned_func
+ returned_func = compose(func, returned_func)
+
+
+def zip_with(func, *iterables):
+
+ if iterables:
+ iterables_min_len = len(iterables[0])
+ else:
+ iterables_min_len = 0
+
+ for iterable in iterables:
+ if iterables_min_len > len(iterable):
+ iterables_min_len = len(iterable)
+
+ for argument_position in range(iterables_min_len):
+ new_args = []
+ for iterable in iterables:
+ new_args.append(iterable[argument_position])
+
+ yield func(*new_args)
+
+
+def cache(func, cache_size):
+
+ @lru_cache(maxsize=cache_size)
+ def func_cached(number):
+ return func(number)
+
+ return func_cached

Георги обнови решението на 15.03.2013 14:41 (преди над 11 години)

from functools import lru_cache
def groupby(func, seq):
grouping_result = {}
for value in seq:
- func_result = func(value)
+ grouping_result.setdefault(func(value), []).append(value)
- if func_result in grouping_result:
- grouping_result[func_result] += [value]
- else:
- grouping_result[func_result] = [value]
-
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
returned_func = lambda x: x
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
+ new_args = zip(*iterables)
- if iterables:
- iterables_min_len = len(iterables[0])
- else:
- iterables_min_len = 0
+ for arg_set in new_args:
+ yield func(*arg_set)
- for iterable in iterables:
- if iterables_min_len > len(iterable):
- iterables_min_len = len(iterable)
- for argument_position in range(iterables_min_len):
- new_args = []
- for iterable in iterables:
- new_args.append(iterable[argument_position])
-
- yield func(*new_args)
-
-
def cache(func, cache_size):
@lru_cache(maxsize=cache_size)
def func_cached(number):
return func(number)
- return func_cached
+ return func_cached

Георги обнови решението на 15.03.2013 15:21 (преди над 11 години)

-from functools import lru_cache
+from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
returned_func = lambda x: x
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
+ _cache = OrderedDict()
- @lru_cache(maxsize=cache_size)
def func_cached(number):
- return func(number)
+ if number not in _cache:
+ _cache[number] = func(number)
+ elif len(_cache) == cache_size:
+ _cache.popitem(False)
+ _cache[number] = func_cached(number)
- return func_cached
+ return _cache[number]
+
+ return func_cached

Георги обнови решението на 15.03.2013 15:22 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
-
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
-
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
returned_func = lambda x: x
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
_cache = OrderedDict()
def func_cached(number):
if number not in _cache:
_cache[number] = func(number)
elif len(_cache) == cache_size:
_cache.popitem(False)
_cache[number] = func_cached(number)
return _cache[number]
return func_cached

Георги обнови решението на 15.03.2013 15:42 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
- returned_func = lambda x: x
+ returned_func = lambda arg: arg
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
- _cache = OrderedDict()
+ cache = OrderedDict()
def func_cached(number):
- if number not in _cache:
- _cache[number] = func(number)
- elif len(_cache) == cache_size:
- _cache.popitem(False)
- _cache[number] = func_cached(number)
+ if number not in cache:
+ cache[number] = func(number)
+ elif len(cache) == cache_size:
+ cache.popitem(False)
+ cache[number] = func_cached(number)
- return _cache[number]
+ return cache[number]
return func_cached

Георги обнови решението на 15.03.2013 16:25 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
- return lambda *args, **kwargs: outter_func(inner_func(*args, **kwargs))
+ return lambda *args, **kwargs: outter_func(inner_func(*args,
+ **kwargs))
returned_func = lambda arg: arg
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
cache = OrderedDict()
- def func_cached(number):
- if number not in cache:
- cache[number] = func(number)
+ def func_cached(*args):
+ if args not in cache:
+ cache[args] = func(*args)
elif len(cache) == cache_size:
cache.popitem(False)
- cache[number] = func_cached(number)
+ cache[args] = func_cached(*args)
- return cache[number]
+ return cache[args]
return func_cached

Георги обнови решението на 15.03.2013 16:36 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args,
**kwargs))
returned_func = lambda arg: arg
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
cache = OrderedDict()
def func_cached(*args):
if args not in cache:
+ if len(cache) == cache_size:
+ cache.popitem(False)
cache[args] = func(*args)
- elif len(cache) == cache_size:
- cache.popitem(False)
- cache[args] = func_cached(*args)
return cache[args]
return func_cached

Георги обнови решението на 15.03.2013 16:39 (преди над 11 години)

from collections import OrderedDict
def groupby(func, seq):
grouping_result = {}
for value in seq:
grouping_result.setdefault(func(value), []).append(value)
return grouping_result
def iterate(func):
def compose(outter_func, inner_func):
return lambda *args, **kwargs: outter_func(inner_func(*args,
**kwargs))
returned_func = lambda arg: arg
while True:
yield returned_func
returned_func = compose(func, returned_func)
def zip_with(func, *iterables):
new_args = zip(*iterables)
for arg_set in new_args:
yield func(*arg_set)
def cache(func, cache_size):
- cache = OrderedDict()
+ _cache = OrderedDict()
def func_cached(*args):
- if args not in cache:
- if len(cache) == cache_size:
- cache.popitem(False)
- cache[args] = func(*args)
+ if args not in _cache:
+ if len(_cache) == cache_size:
+ _cache.popitem(False)
+ _cache[args] = func(*args)
- return cache[args]
+ return _cache[args]
return func_cached