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

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

Към профила на Свилен Андонов

Резултати

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

Код

def iterate(func):
iter = 0
def _inner(arg):
def _recursive(iter, arg):
if iter > 1:
return _recursive(iter - 1, func(arg))
return arg
nonlocal iter
iter += 1
return _recursive(iter, arg)
while True:
yield _inner
def zip_with(func, *iter):
for value in zip(*iter):
yield func(*value)
def groupby(func, iter):
iter = list(iter)
return {key: [value for value in iter if func(value) == key] for key
in set(map(func, iter))}
def cache(func, size):
keys, values = [], []
def func_cached(arg):
if size == 0:
return func(arg)
if arg not in keys and size == len(keys):
del keys[0]
del values[0]
if arg not in keys:
keys.append(arg)
values.append(func(arg))
return values[len(values)-1]
return func_cached

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

..E.................
======================================================================
ERROR: test_cache_function_with_vargs (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-162ry94/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cached() takes 1 positional argument but 3 were given

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

FAILED (errors=1)

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

Свилен обнови решението на 14.03.2013 21:47 (преди над 11 години)

+def iterate(func):
+ iter = 0
+ def _inner(arg):
+ def _recursive(iter, arg):
+ if iter > 1:
+ return _recursive(iter - 1, func(arg))
+ return arg
+ nonlocal iter
+ iter += 1
+ return _recursive(iter, arg)
+ while True:
+ yield _inner
+
+def zip_with(func, *iter):
+ for value in zip(*iter):
+ yield func(*value)
+
+def groupby(func, iter):
+ iter = list(iter)
+ _result = {key: [value for value in iter if func(value) == key] for key
+ in set(map(func, iter))}
+ return _result
+
+def cache(func, size):
+ keys, values = [], []
+ def func_cached(arg):
+ if size == 0:
+ return func(arg)
+ if arg not in keys and size == len(keys):
+ del keys[0]
+ del values[0]
+ if arg not in keys:
+ keys.append(arg)
+ values.append(func(arg))
+ return values[len(values)-1]
+ return cached
+

Свилен обнови решението на 15.03.2013 07:53 (преди над 11 години)

def iterate(func):
iter = 0
def _inner(arg):
def _recursive(iter, arg):
if iter > 1:
return _recursive(iter - 1, func(arg))
return arg
nonlocal iter
iter += 1
return _recursive(iter, arg)
while True:
yield _inner
def zip_with(func, *iter):
for value in zip(*iter):
yield func(*value)
def groupby(func, iter):
iter = list(iter)
- _result = {key: [value for value in iter if func(value) == key] for key
+ return {key: [value for value in iter if func(value) == key] for key
in set(map(func, iter))}
- return _result
def cache(func, size):
keys, values = [], []
def func_cached(arg):
if size == 0:
return func(arg)
if arg not in keys and size == len(keys):
del keys[0]
del values[0]
if arg not in keys:
keys.append(arg)
values.append(func(arg))
return values[len(values)-1]
- return cached
+ return func_cached

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

def iterate(func):
iter = 0
+
def _inner(arg):
def _recursive(iter, arg):
if iter > 1:
return _recursive(iter - 1, func(arg))
return arg
nonlocal iter
iter += 1
return _recursive(iter, arg)
while True:
yield _inner
+
def zip_with(func, *iter):
for value in zip(*iter):
yield func(*value)
+
def groupby(func, iter):
iter = list(iter)
return {key: [value for value in iter if func(value) == key] for key
in set(map(func, iter))}
+
def cache(func, size):
keys, values = [], []
+
def func_cached(arg):
if size == 0:
return func(arg)
if arg not in keys and size == len(keys):
del keys[0]
del values[0]
if arg not in keys:
keys.append(arg)
values.append(func(arg))
return values[len(values)-1]
return func_cached
-

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

def iterate(func):
iter = 0
def _inner(arg):
def _recursive(iter, arg):
if iter > 1:
return _recursive(iter - 1, func(arg))
return arg
nonlocal iter
iter += 1
return _recursive(iter, arg)
while True:
yield _inner
def zip_with(func, *iter):
for value in zip(*iter):
yield func(*value)
def groupby(func, iter):
iter = list(iter)
return {key: [value for value in iter if func(value) == key] for key
- in set(map(func, iter))}
+ in set(map(func, iter))}
def cache(func, size):
keys, values = [], []
def func_cached(arg):
if size == 0:
return func(arg)
if arg not in keys and size == len(keys):
del keys[0]
del values[0]
if arg not in keys:
keys.append(arg)
values.append(func(arg))
return values[len(values)-1]
return func_cached