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

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

Към профила на Вероника Стоилова

Резултати

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

Код

import functools
import collections
from collections import defaultdict
from collections import OrderedDict
def comp(f, g):
return lambda n: f(g(n))
def iterate(func):
f = lambda n: n
yield f
deque_ = collections.deque()
deque_.appendleft(f)
while True:
deque_.appendleft(func)
yield functools.reduce(comp, deque_)
def groupby(func, seq):
result = defaultdict(list)
for elem in seq:
result[func(elem)].append(elem)
return result
def zip_with(func, *iterables):
count_of_iterables = len(iterables)
if not count_of_iterables:
return
else:
length_ = len(iterables[0])
for i in range(0, count_of_iterables):
if len(iterables[i]) < length_:
length_ = len(iterables[i])
for j in range(0, length_):
my_list = []
for i in range(0, count_of_iterables):
my_list.append(iterables[i][j])
yield func(*my_list)
class Function:
current_size_of_cashe = 0
max_size = 0
arguments_and_results = OrderedDict()
def are_args_called(self, args):
if args in self.arguments_and_results.keys():
return self.arguments_and_results[args]
else:
return False
def add_call_with_args(self, args, f_):
if (self.max_size > self.current_size_of_cashe):
self.current_size_of_cashe += 1
else:
self.arguments_and_results.popitem(last=False)
self.arguments_and_results[args] = f_(*args)
def cache(func, cache_size):
f = Function()
f.current_size_of_cache = 0
f.arguments_and_results = OrderedDict()
f.max_size = cache_size
def cache_func(*args):
args_used = f.are_args_called(args)
if args_used:
return f.arguments_and_results[args]
else:
f.add_call_with_args(args, func)
return f.arguments_and_results[args]
return cache_func

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

...E............E...
======================================================================
ERROR: 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-17rhyil/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-17rhyil/solution.py", line 74, in cache_func
    f.add_call_with_args(args, func)
  File "/tmp/d20130408-29081-17rhyil/solution.py", line 59, in add_call_with_args
    self.arguments_and_results.popitem(last=False)
  File "/opt/python3.3/lib/python3.3/collections/__init__.py", line 114, in popitem
    raise KeyError('dictionary is empty')
KeyError: 'dictionary is empty'

======================================================================
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-17rhyil/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-17rhyil/solution.py", line 35, in zip_with
    if len(iterables[i]) < length_:
TypeError: object of type 'itertools.repeat' has no len()

----------------------------------------------------------------------
Ran 20 tests in 0.012s

FAILED (errors=2)

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

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

+import functools
+import collections
+from collections import defaultdict
+from collections import OrderedDict
+
+
+def comp(f, g):
+ return lambda n: f(g(n))
+
+
+def iterate(func):
+ f = lambda n: n
+ yield f
+ deque_ = collections.deque()
+ deque_.appendleft(f)
+ while True:
+ deque_.appendleft(func)
+ yield functools.reduce(comp, deque_)
+
+
+def groupby(func, seq):
+ result = defaultdict(list)
+ for elem in seq:
+ result[func(elem)].append(elem)
+ return result
+
+
+def zip_with(func, *iterables):
+ count_of_iterables = len(iterables)
+ if not count_of_iterables:
+ return
+ else:
+ length_ = len(iterables[0])
+ for i in range(0, count_of_iterables):
+ if len(iterables[i]) < length_:
+ length_ = len(iterables[i])
+ for j in range(0, length_):
+ my_list = []
+ for i in range(0, count_of_iterables):
+ my_list.append(iterables[i][j])
+ yield func(*my_list)
+
+
+class Function:
+ current_size_of_cashe = 0
+ max_size = 0
+ arguments_and_results = OrderedDict()
+
+ def are_args_called(self, args):
+ if args in self.arguments_and_results.keys():
+ return self.arguments_and_results[args]
+ else:
+ return False
+
+ def add_call_with_args(self, args, f_):
+ if (self.max_size > self.current_size_of_cashe):
+ self.current_size_of_cashe += 1
+ else:
+ self.arguments_and_results.popitem(last=False)
+ self.arguments_and_results[args] = f_(*args)
+
+
+def cache(func, cache_size):
+ f = Function()
+ f.current_size_of_cache = 0
+ f.arguments_and_results = OrderedDict()
+ f.max_size = cache_size
+
+ def cache_func(*args):
+ args_used = f.are_args_called(args)
+ if args_used:
+ return f.arguments_and_results[args]
+ else:
+ f.add_call_with_args(args, func)
+ return f.arguments_and_results[args]
+ return cache_func