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

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

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

Резултати

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

Код

from collections import defaultdict, OrderedDict
def groupby(func, seq):
""" Group values in seq on keys generated by func and return a dict. """
grouped_items = defaultdict(list)
for item in seq:
grouped_items[func(item)].append(item)
return dict(grouped_items)
def compose(func1, func2):
""" Return the composition of two functions. """
return lambda arg: func1(func2(arg))
def iterate(func):
"""Return the iteration of `func`.
Generate infinite sequence of compositions of `func`. The nth element
in the sequence is the composition of (n - 1) functions.
The first element is the identity function.
"""
current_composition_func = lambda x: x
while True:
yield current_composition_func
current_composition_func = compose(func, current_composition_func)
def zip_with(func, *iterables):
"""Generate sequence of elements where the nth element is the result of
func applied to the nth elements of all iterables.
"""
if len(iterables) == 0:
return
iterators = [iter(iterable) for iterable in iterables]
while True:
values = [next(iterator) for iterator in iterators]
yield func(*values)
def cache(func, cache_size):
"""Return a function that caches the last `cache_size` results of `func`.
Returns `func` if `cache_size` is <= 0
"""
if cache_size <= 0:
return func
cache_store = OrderedDict()
def cached_func(*args):
if args not in cache_store:
if len(cache_store) >= cache_size:
cache_store.popitem(False)
cache_store[args] = func(*args)
return cache_store[args]
return cached_func

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

....................
----------------------------------------------------------------------
Ran 20 tests in 0.010s

OK

История (4 версии и 5 коментара)

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

+from collections import defaultdict, OrderedDict
+
+
+def groupby(func, seq):
+ """ Group values in seq on keys generated by func and return a dict. """
+ grouped_items = defaultdict(list)
+
+ for item in seq:
+ grouped_items[func(item)].append(item)
+
+ return dict(grouped_items)
+
+
+def compose(func1, func2):
+ """ Return the composition of two functions. """
+ return lambda arg: func1(func2(arg))
+
+
+def iterate(func):
+ """Return the iteration of a function.
+
+ Generate infinite sequence of compositions of func. The nth element in the
+ sequence is the composition of (n - 1) functions.
+
+ The first element is the identity function.
+
+ """
+ current_composition_func = lambda x: x
+
+ while True:
+ yield current_composition_func
+ current_composition_func = compose(func, current_composition_func)
+
+
+def zip_with(func, *iterables):
+ """Generate sequence of elements where the nth element is the result of
+ func applied to the nth elements of all iterables.
+
+ """
+ iterators = [iter(iterable) for iterable in iterables]
+
+ while True:
+ values = [next(iterator) for iterator in iterators]
+ yield func(*values)
+
+
+def cache(func, cache_size):
+ """Return a function that caches the last `cache_size` results of `func`.
+
+ """
+ cache_store = OrderedDict()
+
+ def cached_func(*args):
+ if args in cache_store:
+ return cache_store[args]
+
+ if len(cache_store) == cache_size:
+ cache_store.popitem(False)
+
+ cache_store[args] = func(*args)
+ return cache_store[args]
+
+ return cached_func

Георги обнови решението на 13.03.2013 22:29 (преди около 11 години)

from collections import defaultdict, OrderedDict
def groupby(func, seq):
""" Group values in seq on keys generated by func and return a dict. """
grouped_items = defaultdict(list)
for item in seq:
grouped_items[func(item)].append(item)
return dict(grouped_items)
def compose(func1, func2):
""" Return the composition of two functions. """
return lambda arg: func1(func2(arg))
def iterate(func):
"""Return the iteration of a function.
Generate infinite sequence of compositions of func. The nth element in the
sequence is the composition of (n - 1) functions.
The first element is the identity function.
"""
current_composition_func = lambda x: x
while True:
yield current_composition_func
current_composition_func = compose(func, current_composition_func)
def zip_with(func, *iterables):
"""Generate sequence of elements where the nth element is the result of
func applied to the nth elements of all iterables.
"""
+ if len(iterables) == 0:
+ return []
+
iterators = [iter(iterable) for iterable in iterables]
while True:
values = [next(iterator) for iterator in iterators]
yield func(*values)
def cache(func, cache_size):
"""Return a function that caches the last `cache_size` results of `func`.
"""
cache_store = OrderedDict()
def cached_func(*args):
if args in cache_store:
return cache_store[args]
if len(cache_store) == cache_size:
cache_store.popitem(False)
cache_store[args] = func(*args)
return cache_store[args]
return cached_func

Георги обнови решението на 13.03.2013 22:31 (преди около 11 години)

from collections import defaultdict, OrderedDict
def groupby(func, seq):
""" Group values in seq on keys generated by func and return a dict. """
grouped_items = defaultdict(list)
for item in seq:
grouped_items[func(item)].append(item)
return dict(grouped_items)
def compose(func1, func2):
""" Return the composition of two functions. """
return lambda arg: func1(func2(arg))
def iterate(func):
"""Return the iteration of a function.
Generate infinite sequence of compositions of func. The nth element in the
sequence is the composition of (n - 1) functions.
The first element is the identity function.
"""
current_composition_func = lambda x: x
while True:
yield current_composition_func
current_composition_func = compose(func, current_composition_func)
def zip_with(func, *iterables):
"""Generate sequence of elements where the nth element is the result of
func applied to the nth elements of all iterables.
"""
if len(iterables) == 0:
- return []
+ return
iterators = [iter(iterable) for iterable in iterables]
while True:
values = [next(iterator) for iterator in iterators]
yield func(*values)
def cache(func, cache_size):
"""Return a function that caches the last `cache_size` results of `func`.
"""
cache_store = OrderedDict()
def cached_func(*args):
if args in cache_store:
return cache_store[args]
if len(cache_store) == cache_size:
cache_store.popitem(False)
cache_store[args] = func(*args)
return cache_store[args]
return cached_func

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

from collections import defaultdict, OrderedDict
def groupby(func, seq):
""" Group values in seq on keys generated by func and return a dict. """
grouped_items = defaultdict(list)
for item in seq:
grouped_items[func(item)].append(item)
return dict(grouped_items)
def compose(func1, func2):
""" Return the composition of two functions. """
return lambda arg: func1(func2(arg))
def iterate(func):
- """Return the iteration of a function.
+ """Return the iteration of `func`.
- Generate infinite sequence of compositions of func. The nth element in the
- sequence is the composition of (n - 1) functions.
+ Generate infinite sequence of compositions of `func`. The nth element
+ in the sequence is the composition of (n - 1) functions.
The first element is the identity function.
"""
current_composition_func = lambda x: x
while True:
yield current_composition_func
current_composition_func = compose(func, current_composition_func)
def zip_with(func, *iterables):
"""Generate sequence of elements where the nth element is the result of
func applied to the nth elements of all iterables.
"""
if len(iterables) == 0:
return
iterators = [iter(iterable) for iterable in iterables]
while True:
values = [next(iterator) for iterator in iterators]
yield func(*values)
def cache(func, cache_size):
"""Return a function that caches the last `cache_size` results of `func`.
+ Returns `func` if `cache_size` is <= 0
+
"""
+ if cache_size <= 0:
+ return func
+
cache_store = OrderedDict()
def cached_func(*args):
- if args in cache_store:
- return cache_store[args]
+ if args not in cache_store:
+ if len(cache_store) >= cache_size:
+ cache_store.popitem(False)
- if len(cache_store) == cache_size:
- cache_store.popitem(False)
+ cache_store[args] = func(*args)
- cache_store[args] = func(*args)
return cache_store[args]
return cached_func