Решение на Четири функции от Илия Ячев

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

Към профила на Илия Ячев

Резултати

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

Код

def groupby(func, seq):
result = {func(x): [] for x in seq}
for x in seq:
result[func(x)].append(x)
return result
def iterate(func):
return Iterator(func)
class Iterator():
def __init__(self, func):
self.func = func
def __iter__(self):
return self
def __next__(self):
try:
current = self.current
self.current = lambda x: self.func(current(x))
except AttributeError:
self.current = lambda x: x
return self.current
def zip_with(func, *iterables):
return [func(*args) for args in zip(*iterables)]
def cache(func, cache_size):
if not hasattr(cache, 'arguments'):
cache.arguments = []
cache.values = []
def func_cached(*args):
if args in cache.arguments:
index = cache.arguments.index(args)
cache.arguments.insert(0, cache.arguments.pop(index))
cache.values.insert(0, cache.values.pop(index))
return cache.values[0]
else:
value = func(*args)
cache.arguments.append(args)
cache.values.append(value)
return value
return func_cached

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

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

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

======================================================================
FAIL: test_cache_size_is_respected (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-lmst6f/test.py", line 149, in test_cache_size_is_respected
    self.assertEqual(4, call_count)
AssertionError: 4 != 3

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

FAILED (failures=3)

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

Илия обнови решението на 15.03.2013 23:22 (преди над 11 години)

+def groupby(func, seq):
+ result = {func(x): [] for x in seq}
+ for x in seq:
+ result[func(x)].append(x)
+ return result
+
+
+def iterate(func):
+ return Iterator(func)
+
+
+class Iterator():
+ def __init__(self, func):
+ self.func = func
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ try:
+ current = self.current
+ self.current = lambda x: self.func(current(x))
+ except AttributeError:
+ self.current = lambda x: x
+ return self.current
+
+
+def zip_with(func, *iterables):
+ # return map(lambda *args: func(*args), zip(*iterables))
+ return [func(*args) for args in zip(*iterables)]
+
+
+def cache(func, cache_size):
+ if not hasattr(cache, 'arguments'):
+ cache.arguments = []
+ cache.values = []
+
+ def func_cached(args):
+ if args in cache.arguments:
+ index = cache.arguments.index(args)
+ cache.arguments.insert(0, cache.arguments.pop(index))
+ cache.values.insert(0, cache.values.pop(index))
+ return cache.values[0]
+ else:
+ value = func(args)
+ cache.arguments.append(args)
+ cache.values.append(value)
+ return value
+ return func_cached

Илия обнови решението на 15.03.2013 23:23 (преди над 11 години)

def groupby(func, seq):
result = {func(x): [] for x in seq}
for x in seq:
result[func(x)].append(x)
return result
def iterate(func):
return Iterator(func)
class Iterator():
def __init__(self, func):
self.func = func
def __iter__(self):
return self
def __next__(self):
try:
current = self.current
self.current = lambda x: self.func(current(x))
except AttributeError:
self.current = lambda x: x
return self.current
def zip_with(func, *iterables):
- # return map(lambda *args: func(*args), zip(*iterables))
return [func(*args) for args in zip(*iterables)]
def cache(func, cache_size):
if not hasattr(cache, 'arguments'):
cache.arguments = []
cache.values = []
def func_cached(args):
if args in cache.arguments:
index = cache.arguments.index(args)
cache.arguments.insert(0, cache.arguments.pop(index))
cache.values.insert(0, cache.values.pop(index))
return cache.values[0]
else:
value = func(args)
cache.arguments.append(args)
cache.values.append(value)
return value
return func_cached

Илия обнови решението на 15.03.2013 23:27 (преди над 11 години)

def groupby(func, seq):
result = {func(x): [] for x in seq}
for x in seq:
result[func(x)].append(x)
return result
def iterate(func):
return Iterator(func)
class Iterator():
def __init__(self, func):
self.func = func
def __iter__(self):
return self
def __next__(self):
try:
current = self.current
self.current = lambda x: self.func(current(x))
except AttributeError:
self.current = lambda x: x
return self.current
def zip_with(func, *iterables):
return [func(*args) for args in zip(*iterables)]
def cache(func, cache_size):
if not hasattr(cache, 'arguments'):
cache.arguments = []
cache.values = []
- def func_cached(args):
+ def func_cached(*args):
if args in cache.arguments:
index = cache.arguments.index(args)
cache.arguments.insert(0, cache.arguments.pop(index))
cache.values.insert(0, cache.values.pop(index))
return cache.values[0]
else:
- value = func(args)
+ value = func(*args)
cache.arguments.append(args)
cache.values.append(value)
return value
return func_cached