Решение на Четири функции от Красимира Божанова

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

Към профила на Красимира Божанова

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
dictionary = {}
for x in seq:
dictionary.setdefault(func(x), []).append(x)
return dictionary
def iterate(func):
def generate_func(iter_func):
return lambda x: func(iter_func(x))
iter_func = lambda x: x
while True:
yield iter_func
iter_func = generate_func(iter_func)
def zip_with(func, *iterables):
if iterables:
for x in zip(*iterables):
yield func(*x)
def cache(func, cache_size):
cache_dict = OrderedDict()
def cache_memory(*args):
if args in cache_dict:
return cache_dict[args]
first_func_call = func(*args)
if cache_size:
if len(cache_dict) >= cache_size:
cache_dict.popitem(last=False)
cache_dict[args] = first_func_call
return first_func_call
return cache_memory

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

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

OK

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

Красимира обнови решението на 14.03.2013 00:38 (преди около 11 години)

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ dictionary = {}
+ for x in seq:
+ dictionary.setdefault(func(x), []).append(x)
+ return dictionary
+
+
+def iterate(func):
+ def generate_func(iter_func):
+ return lambda x: func(iter_func(x))
+ iter_func = lambda x: x
+ while True:
+ yield iter_func
+ iter_func = generate_func(iter_func)
+
+
+def zip_with(func, *iterables):
+ if (len(iterables)):
+ min_length = min([len(x) for x in iterables])
+ for i in range(min_length):
+ yield func(*[x[i] for x in iterables])
+
+
+def cache(func, cache_size):
+ cache_dict = OrderedDict()
+
+ def cache_memory(*args):
+ if args in cache_dict:
+ return cache_dict[args]
+
+ if len(cache_dict) >= cache_size:
+ cache_dict.popitem(last=False)
+ first_func_call = func(*args)
+ cache_dict[args] = first_func_call
+ return first_func_call
+
+ return cache_memory

Красимира обнови решението на 14.03.2013 00:40 (преди около 11 години)

from collections import OrderedDict
def groupby(func, seq):
dictionary = {}
for x in seq:
dictionary.setdefault(func(x), []).append(x)
return dictionary
def iterate(func):
def generate_func(iter_func):
- return lambda x: func(iter_func(x))
+ return lambda x: func(iter_func(x))
iter_func = lambda x: x
while True:
yield iter_func
iter_func = generate_func(iter_func)
def zip_with(func, *iterables):
if (len(iterables)):
min_length = min([len(x) for x in iterables])
for i in range(min_length):
yield func(*[x[i] for x in iterables])
def cache(func, cache_size):
cache_dict = OrderedDict()
def cache_memory(*args):
if args in cache_dict:
return cache_dict[args]
if len(cache_dict) >= cache_size:
cache_dict.popitem(last=False)
first_func_call = func(*args)
cache_dict[args] = first_func_call
return first_func_call
return cache_memory

Красимира обнови решението на 14.03.2013 21:34 (преди около 11 години)

from collections import OrderedDict
def groupby(func, seq):
dictionary = {}
for x in seq:
dictionary.setdefault(func(x), []).append(x)
return dictionary
def iterate(func):
def generate_func(iter_func):
return lambda x: func(iter_func(x))
iter_func = lambda x: x
while True:
yield iter_func
iter_func = generate_func(iter_func)
def zip_with(func, *iterables):
- if (len(iterables)):
+ if iterables:
min_length = min([len(x) for x in iterables])
for i in range(min_length):
yield func(*[x[i] for x in iterables])
def cache(func, cache_size):
cache_dict = OrderedDict()
def cache_memory(*args):
if args in cache_dict:
return cache_dict[args]
if len(cache_dict) >= cache_size:
cache_dict.popitem(last=False)
first_func_call = func(*args)
cache_dict[args] = first_func_call
return first_func_call
return cache_memory
+

Красимира обнови решението на 15.03.2013 20:01 (преди около 11 години)

from collections import OrderedDict
def groupby(func, seq):
dictionary = {}
for x in seq:
dictionary.setdefault(func(x), []).append(x)
return dictionary
def iterate(func):
def generate_func(iter_func):
return lambda x: func(iter_func(x))
+
iter_func = lambda x: x
while True:
yield iter_func
iter_func = generate_func(iter_func)
def zip_with(func, *iterables):
if iterables:
- min_length = min([len(x) for x in iterables])
- for i in range(min_length):
- yield func(*[x[i] for x in iterables])
+ for x in zip(*iterables):
+ yield func(*x)
+
-
def cache(func, cache_size):
cache_dict = OrderedDict()
def cache_memory(*args):
if args in cache_dict:
return cache_dict[args]
-
- if len(cache_dict) >= cache_size:
- cache_dict.popitem(last=False)
first_func_call = func(*args)
- cache_dict[args] = first_func_call
+ if cache_size:
+ if len(cache_dict) >= cache_size:
+ cache_dict.popitem(last=False)
+ cache_dict[args] = first_func_call
return first_func_call
-
- return cache_memory
+
-
+ return cache_memory