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

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

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

Резултати

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

Код

import collections
def zip_with(func, *iterables):
zipped = zip(*iterables)
list_of_zip = list(zipped)
for zipvalue in list_of_zip:
zipped_by_func = func(*zipvalue)
yield zipped_by_func
def iterate(func):
def compose(f, g):
return lambda x: f(g(x))
iterate_result = lambda y: y
while(True):
yield iterate_result
iterate_result = compose(func, iterate_result)
def groupby(func, seq):
keys = {func(i) for i in seq}
return {key: list(filter(lambda i: func(i) == key, seq))
for key in keys}
def cache(func, cache_size):
cached_values = collections.OrderedDict()
def func_cached(*args):
if args not in cached_values:
if len(cached_values) >= cache_size:
cached_values.popitem(False)
cached_values[args] = func(*args)
return cached_values[args]
if cache_size <= 0:
return func
else:
return func_cached

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

....................
----------------------------------------------------------------------
Ran 20 tests in 0.011s

OK

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

Христина обнови решението на 14.03.2013 23:22 (преди над 11 години)

+def zip_with(func, *iterables):
+ zipped = zip(*iterables)
+ list_of_zip = list(zipped)
+
+ for zipvalue in list_of_zip:
+ zipped_by_func = func(*zipvalue)
+ yield zipped_by_func
+
+
+def iterate(func):
+ def compose(f, g):
+ return lambda x: f(g(x))
+
+ res_func = lambda y: y
+ while(True):
+ yield res_func
+ res_func = compose(func, res_func)
+
+
+def groupby(func, seq):
+ keys = {func(i) for i in seq}
+ return {key: list(filter(lambda i: func(i) == key, seq))
+ for key in keys}
+
+
+def cache(func, cache_size):
+ cached_args = []
+ cached_values = {}
+
+ def update_cache(all_args, values):
+ nonlocal cached_args
+ if len(cached_args) >= cache_size:
+ del cached_values[cached_args[0]]
+ cached_args = cached_args[1:]
+
+ cached_args.append(all_args)
+
+ def func_cached(*args):
+ all_args = args
+
+ if all_args not in cached_values:
+ cached_values[all_args] = func(*args)
+ update_cache(all_args, cached_values[all_args])
+
+ return cached_values[all_args]
+
+ return func_cached

Христина обнови решението на 15.03.2013 15:02 (преди над 11 години)

+import collections
+
def zip_with(func, *iterables):
zipped = zip(*iterables)
list_of_zip = list(zipped)
for zipvalue in list_of_zip:
zipped_by_func = func(*zipvalue)
yield zipped_by_func
def iterate(func):
def compose(f, g):
return lambda x: f(g(x))
- res_func = lambda y: y
+ iterate_result = lambda y: y
while(True):
- yield res_func
- res_func = compose(func, res_func)
+ yield iterate_result
+ iterate_result = compose(func, iterate_result)
def groupby(func, seq):
keys = {func(i) for i in seq}
return {key: list(filter(lambda i: func(i) == key, seq))
for key in keys}
def cache(func, cache_size):
- cached_args = []
- cached_values = {}
+ cached_values = collections.OrderedDict()
- def update_cache(all_args, values):
- nonlocal cached_args
- if len(cached_args) >= cache_size:
- del cached_values[cached_args[0]]
- cached_args = cached_args[1:]
-
- cached_args.append(all_args)
-
def func_cached(*args):
- all_args = args
+ if args not in cached_values:
+ if len(cached_values) >= cache_size:
+ cached_values.popitem(False)
- if all_args not in cached_values:
- cached_values[all_args] = func(*args)
- update_cache(all_args, cached_values[all_args])
+ cached_values[args] = func(*args)
- return cached_values[all_args]
+ return cached_values[args]
- return func_cached
+ if cache_size <= 0:
+ return func
+ else:
+ return func_cached