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

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

Към профила на Станислав Гатев

Резултати

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

Код

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def wrap(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = wrap(current_func)
def zip_with(func, *iterables):
if len(iterables) == 0:
return []
for i in range(0, min(map(lambda x: len(list(x)), iterables))):
yield func(*[iterable[i] for iterable in map(list, iterables)])
def cache(func, cache_size):
func.cache = []
def cached_func(*args):
for cached_args, cached_result in func.cache:
if cached_args == args:
return cached_result
result = func(*args)
func.cache = [(args, result)] + func.cache[:cache_size - 1]
return result
return cached_func

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

F...............E...
======================================================================
ERROR: test_zip_with_infinite_sequence (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-1bt0lie/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1bt0lie/solution.py", line 19, in zip_with
    for i in range(0, min(map(lambda x: len(list(x)), iterables))):
  File "/tmp/d20130408-29081-1bt0lie/solution.py", line 19, in <lambda>
    for i in range(0, min(map(lambda x: len(list(x)), iterables))):
MemoryError

======================================================================
FAIL: test_cache_cache_is_not_global (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-1bt0lie/test.py", line 133, in test_cache_cache_is_not_global
    self.assertEqual(2, call_count)
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 20 tests in 5.169s

FAILED (failures=1, errors=1)

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

Станислав обнови решението на 14.03.2013 20:06 (преди около 11 години)

+def groupby(func, seq):
+ return {group: [n for n in seq if func(n) == group]
+ for group in set(map(func, seq))}
+
+
+def iterate(func):
+ def compose(inner_func):
+ return lambda x: func(inner_func(x))
+
+ current_func = lambda x: x
+ while True:
+ yield current_func
+ current_func = compose(current_func)
+
+
+def zip_with(func, *iterables):
+ min_iterables_len = min(map(len, iterables)) if len(iterables) else 0
+ for i in range(0, min_iterables_len):
+ yield func(*[iterable[i] for iterable in iterables])
+
+
+def cache(func, cache_size):
+ class CachedFunc():
+ def __init__(self):
+ self.cache = []
+
+ def __call__(self, *args):
+ for arguments, result in self.cache:
+ if arguments == args:
+ return result
+ return self.__non_cached_call(*args)
+
+ def __non_cached_call(self, *args):
+ result = func(*args)
+ self.cache = [(args, result)] + self.cache[:cache_size - 1]
+ return result
+ return CachedFunc()

Станислав обнови решението на 15.03.2013 15:39 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def compose(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = compose(current_func)
def zip_with(func, *iterables):
- min_iterables_len = min(map(len, iterables)) if len(iterables) else 0
+ min_iterables_len = min(map(lambda x: len(list(x)), iterables)) if len(list(iterables)) else 0
for i in range(0, min_iterables_len):
yield func(*[iterable[i] for iterable in iterables])
def cache(func, cache_size):
- class CachedFunc():
- def __init__(self):
- self.cache = []
+ func.cache = []
- def __call__(self, *args):
- for arguments, result in self.cache:
- if arguments == args:
- return result
- return self.__non_cached_call(*args)
+ def cached_func(*args):
+ for arguments, result in func.cache:
+ if arguments == args:
+ return result
+ result = func(*args)
+ func.cache = [(args, result)] + func.cache[:cache_size - 1]
+ return result
- def __non_cached_call(self, *args):
- result = func(*args)
+ return cached_func
- self.cache = [(args, result)] + self.cache[:cache_size - 1]
- return result
- return CachedFunc()

Станислав обнови решението на 15.03.2013 21:44 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def compose(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = compose(current_func)
def zip_with(func, *iterables):
- min_iterables_len = min(map(lambda x: len(list(x)), iterables)) if len(list(iterables)) else 0
+ min_iterables_len = min(map(lambda x: len(list(x)),
+ iterables)) if len(list(iterables)) else 0
for i in range(0, min_iterables_len):
yield func(*[iterable[i] for iterable in iterables])
def cache(func, cache_size):
- func.cache = []
+ func.cache = []
- def cached_func(*args):
- for arguments, result in func.cache:
- if arguments == args:
- return result
- result = func(*args)
- func.cache = [(args, result)] + func.cache[:cache_size - 1]
- return result
+ def cached_func(*args):
+ for arguments, result in func.cache:
+ if arguments == args:
+ return result
+ result = func(*args)
+ func.cache = [(args, result)] + func.cache[:cache_size - 1]
+ return result
- return cached_func
+ return cached_func

Станислав обнови решението на 15.03.2013 22:48 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
- def compose(inner_func):
+ def wrap(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
- current_func = compose(current_func)
+ current_func = wrap(current_func)
def zip_with(func, *iterables):
- min_iterables_len = min(map(lambda x: len(list(x)),
- iterables)) if len(list(iterables)) else 0
- for i in range(0, min_iterables_len):
+ if len(list(iterables)) == 0:
+ return []
+ for i in range(0, min(map(lambda x: len(list(x)), list(iterables)))):
yield func(*[iterable[i] for iterable in iterables])
def cache(func, cache_size):
func.cache = []
def cached_func(*args):
- for arguments, result in func.cache:
- if arguments == args:
- return result
+ for cached_args, cached_result in func.cache:
+ if cached_args == args:
+ return cached_result
result = func(*args)
func.cache = [(args, result)] + func.cache[:cache_size - 1]
return result
return cached_func

Станислав обнови решението на 15.03.2013 22:49 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def wrap(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = wrap(current_func)
def zip_with(func, *iterables):
if len(list(iterables)) == 0:
return []
for i in range(0, min(map(lambda x: len(list(x)), list(iterables)))):
- yield func(*[iterable[i] for iterable in iterables])
+ yield func(*[list(iterable)[i] for iterable in iterables])
def cache(func, cache_size):
func.cache = []
def cached_func(*args):
for cached_args, cached_result in func.cache:
if cached_args == args:
return cached_result
result = func(*args)
func.cache = [(args, result)] + func.cache[:cache_size - 1]
return result
return cached_func

Станислав обнови решението на 15.03.2013 22:56 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def wrap(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = wrap(current_func)
def zip_with(func, *iterables):
if len(list(iterables)) == 0:
return []
for i in range(0, min(map(lambda x: len(list(x)), list(iterables)))):
- yield func(*[list(iterable)[i] for iterable in iterables])
+ yield func(*[iterable[i] for iterable in map(list, iterables)])
def cache(func, cache_size):
func.cache = []
def cached_func(*args):
for cached_args, cached_result in func.cache:
if cached_args == args:
return cached_result
result = func(*args)
func.cache = [(args, result)] + func.cache[:cache_size - 1]
return result
return cached_func

Станислав обнови решението на 15.03.2013 23:06 (преди около 11 години)

def groupby(func, seq):
return {group: [n for n in seq if func(n) == group]
for group in set(map(func, seq))}
def iterate(func):
def wrap(inner_func):
return lambda x: func(inner_func(x))
current_func = lambda x: x
while True:
yield current_func
current_func = wrap(current_func)
def zip_with(func, *iterables):
- if len(list(iterables)) == 0:
+ if len(iterables) == 0:
return []
- for i in range(0, min(map(lambda x: len(list(x)), list(iterables)))):
+ for i in range(0, min(map(lambda x: len(list(x)), iterables))):
yield func(*[iterable[i] for iterable in map(list, iterables)])
def cache(func, cache_size):
func.cache = []
def cached_func(*args):
for cached_args, cached_result in func.cache:
if cached_args == args:
return cached_result
result = func(*args)
func.cache = [(args, result)] + func.cache[:cache_size - 1]
return result
return cached_func