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

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

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

Резултати

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

Код

def groupby(func, seq):
keys = set()
dictionary = {}
for i in seq:
keys.add(func(i))
for key in keys:
dictionary[key] = [x for x in seq if func(x) == key]
return dictionary
def iterate(func):
def compose(x, counter, func):
result = x
for i in range(counter):
result = func(result)
return result
counter = 0
while True:
yield lambda x: compose(x, counter, func)
counter += 1
def zip_with(func, *iterables):
if len(iterables):
min_length = len(iterables[0])
for iterable in iterables:
if min_length > len(iterable):
min_length = len(iterable)
arguments = []
for i in range(min_length):
for j in range(len(iterables)):
arguments.append(iterables[j][i])
yield func(*arguments)
arguments.clear()
else:
yield
from collections import OrderedDict
def cache(func, cache_size):
cached = OrderedDict()
def func_cached(*arguments):
if arguments in cached:
return cached[arguments]
else:
cached[arguments] = func(*arguments)
if len(cached) > cache_size:
cached.popitem(last=False)
return cached[arguments]
return func_cached

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

...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-1r68s7/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1r68s7/solution.py", line 54, in func_cached
    return cached[arguments]
KeyError: (42,)

======================================================================
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-1r68s7/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1r68s7/solution.py", line 29, in zip_with
    if min_length > len(iterable):
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-1r68s7/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-1r68s7/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-1r68s7/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-1r68s7/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.014s

FAILED (failures=4, errors=2)

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

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

+def groupby(func, seq):
+ keys = set()
+ dictionary = {}
+
+ for i in seq:
+ keys.add(func(i))
+ for key in keys:
+ dictionary[key] = [x for x in seq if func(x) == key]
+ return dictionary
+
+
+def iterate(func):
+ def compose(x, counter, func):
+ result = x
+ for i in range(counter):
+ result = func(result)
+ return result
+
+ counter = 0
+ while True:
+ yield lambda x: compose(x, counter, func)
+ counter += 1
+
+
+def zip_with(func, *iterables):
+ if len(iterables):
+ min_length = len(iterables[0])
+ for iterable in iterables:
+ if min_length > len(iterable):
+ min_length = len(iterable)
+ arguments = []
+ for i in range(min_length):
+ for j in range(len(iterables)):
+ arguments.append(iterables[j][i])
+ yield func(*arguments)
+ arguments.clear()
+ else:
+ yield

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

def groupby(func, seq):
keys = set()
dictionary = {}
for i in seq:
keys.add(func(i))
for key in keys:
dictionary[key] = [x for x in seq if func(x) == key]
return dictionary
def iterate(func):
def compose(x, counter, func):
result = x
for i in range(counter):
result = func(result)
return result
counter = 0
while True:
yield lambda x: compose(x, counter, func)
counter += 1
def zip_with(func, *iterables):
if len(iterables):
min_length = len(iterables[0])
for iterable in iterables:
if min_length > len(iterable):
min_length = len(iterable)
arguments = []
for i in range(min_length):
for j in range(len(iterables)):
arguments.append(iterables[j][i])
yield func(*arguments)
arguments.clear()
else:
yield
+
+
+from collections import OrderedDict
+
+
+def cache(func, cache_size):
+ cached = OrderedDict()
+
+ def func_cached(*arguments):
+ if arguments in cached:
+ return cached[arguments]
+ else:
+ cached[arguments] = func(*arguments)
+ if len(cached) > cache_size:
+ cached.popitem(last=False)
+ return cached[arguments]
+ return func_cached