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

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

Към профила на Георги Горанов

Резултати

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

Код

from collections import OrderedDict
def groupby(func, seq):
group = {}
for key in set(map(func, seq)):
group[key] = [x for x in seq if func(x) == key]
return (group)
def iterate(func):
iterator = 0
while True:
yield lambda x: composition(x, iterator, func)
iterator += 1
def composition(x, iterator, func):
result = x
for i in range(iterator):
result = func(result)
return result
def zip_with(func, *iterables):
if len(iterables):
iterables_count = len(iterables)
length = len(iterables[0])
for iterable in iterables:
if len(iterable) < length:
length = len(iterable)
call_args = []
for iterator in range(length):
for it in range(iterables_count):
call_args.append(iterables[it][iterator])
yield func(*call_args)
call_args.clear()
else:
yield
def cache(func, cache_size):
cache_dict = OrderedDict()
def controller(*args):
if len(cache_dict) > cache_size - 1:
cache_dict.popitem(last=False)
if cache_dict.__contains__(args):
return cache_dict[args]
else:
cache_dict[args] = func(*args)
return cache_dict[args]
return controller

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

...E.......FFF.FE...
======================================================================
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-l6aolt/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-l6aolt/solution.py", line 47, in controller
    cache_dict.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-l6aolt/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-l6aolt/solution.py", line 30, in zip_with
    if len(iterable) < length:
TypeError: object of type 'itertools.repeat' has no len()

======================================================================
FAIL: test_iterate_out_of_order_calls (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-l6aolt/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: test_iterate_out_of_order_calls_again (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-l6aolt/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: test_iterate_out_of_order_calls_yet_again (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-l6aolt/test.py", line 53, in test_iterate_out_of_order_calls
    self.assertEqual(1 * 'eggs', f0('eggs'))
AssertionError: 'eggs' != 'eggseggseggseggseggseggseggseggs'
- eggs
+ eggseggseggseggseggseggseggseggs


======================================================================
FAIL: test_zip_with_empty (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-l6aolt/test.py", line 67, in test_zip_with_empty
    self.assertEqual(expected, list(actual))
AssertionError: Lists differ: [] != [None]

Second list contains 1 additional elements.
First extra element 0:
None

- []
+ [None]

----------------------------------------------------------------------
Ran 20 tests in 0.015s

FAILED (failures=4, errors=2)

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

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

+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ group = {}
+ for key in set(map(func, seq)):
+ group[key] = [x for x in seq if func(x) == key]
+ return (group)
+
+
+def iterate(func):
+ iterator = 0
+ while True:
+ yield lambda x: composition(x, iterator, func)
+ iterator += 1
+
+
+def composition(x, iterator, func):
+ result = x
+ for i in range(iterator):
+ result = func(result)
+ return result
+
+
+def zip_with(func, *iterables):
+ if len(iterables):
+ iterables_count = len(iterables)
+ length = len(iterables[0])
+ for iterable in iterables:
+ if len(iterable) < length:
+ length = len(iterable)
+ call_args = []
+ for iterator in range(length):
+ for it in range(iterables_count):
+ call_args.append(iterables[it][iterator])
+ yield func(*call_args)
+ call_args.clear()
+ else:
+ yield
+
+
+def cache(func, cache_size):
+ cache_dict = OrderedDict()
+
+ def controller(*args):
+ if len(cache_dict) > cache_size - 1:
+ cache_dict.popitem(last=False)
+ if cache_dict.__contains__(args):
+ return cache_dict[args]
+ else:
+ cache_dict[args] = func(*args)
+ return cache_dict[args]
+ return controller