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

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

Към профила на Радослав Рачев

Резултати

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

Код

from itertools import count
from collections import OrderedDict
def groupby(func, seq):
return {result: [item for item in seq if func(item) == result] for
result in set(map(func, seq))}
def iterate(func):
def iterate_times(times, x):
result = x
for i in range(times):
result = func(result)
return result
for times in count():
yield lambda x: iterate_times(times, x)
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def func_cached(*args):
if args in store:
return store[args]
result = func(*args)
if cache_size:
if len(store) == cache_size:
store.popitem(False)
store[args] = result
return result
return func_cached

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

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


----------------------------------------------------------------------
Ran 20 tests in 0.016s

FAILED (failures=3)

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

Радослав обнови решението на 14.03.2013 23:22 (преди над 11 години)

+from itertools import count
+from collections import OrderedDict
+
+
+def groupby(func, seq):
+ return {result: [item for item in seq if func(item) == result] for
+ result in set(map(func, seq))}
+
+
+def iterate(func):
+ pass
+
+
+def zip_with(func, *iterables):
+ return (func(*args) for args in zip(*iterables))
+
+
+def cache(func, cache_size):
+ store = OrderedDict()
+
+ def func_cached(*args):
+ if args in store:
+ return store[args]
+
+ result = func(*args)
+
+ if cache_size:
+ if len(store) == cache_size:
+ store.popitem(False)
+ store[args] = result
+
+ return result
+
+ return func_cached

Радослав обнови решението на 14.03.2013 23:35 (преди над 11 години)

from itertools import count
from collections import OrderedDict
def groupby(func, seq):
return {result: [item for item in seq if func(item) == result] for
result in set(map(func, seq))}
def iterate(func):
- pass
+ return lambda x: x
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def func_cached(*args):
if args in store:
return store[args]
result = func(*args)
if cache_size:
if len(store) == cache_size:
store.popitem(False)
store[args] = result
return result
return func_cached

Радослав обнови решението на 14.03.2013 23:46 (преди над 11 години)

from itertools import count
from collections import OrderedDict
def groupby(func, seq):
return {result: [item for item in seq if func(item) == result] for
result in set(map(func, seq))}
def iterate(func):
- return lambda x: x
+ def iterate_times(times, x):
+ result = x
+
+ for i in range(times):
+ result = func(result)
+
+ return result
+
+ for times in count():
+ yield lambda x: iterate_times(times, x)
def zip_with(func, *iterables):
return (func(*args) for args in zip(*iterables))
def cache(func, cache_size):
store = OrderedDict()
def func_cached(*args):
if args in store:
return store[args]
result = func(*args)
if cache_size:
if len(store) == cache_size:
store.popitem(False)
store[args] = result
return result
return func_cached