Решение на Четири функции от Христо Хърсев

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

Към профила на Христо Хърсев

Резултати

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

Код

from itertools import chain
from collections import OrderedDict
def groupby(func, seq):
result = {}
for elem in seq:
if func(elem) in result:
result.update({func(elem): result[func(elem)] + [elem]})
else:
result.update({func(elem): [elem]})
return result
def compose(func, times):
if times == 1:
return func
return(lambda x: func(compose(func, times - 1)(x)))
def iterate(func):
first_call = (lambda func: func)
yield first_call
composition_count = 0
while True:
composition_count += 1
next_call = (lambda *args: (compose(func, composition_count)(*args)))
yield(next_call)
def zip_with(func, *iterables):
if iterables == ():
return([])
iterable_with_min_length = (min(map(len, iterables)))
iterator = 0
while iterator != iterable_with_min_length:
current = []
for item in iterables:
current.append(item[iterator])
yield(func(*(chain(current))))
iterator += 1
def cache(func, cache_size):
cached = OrderedDict()
def func_cached(arg):
if not(cached.get(arg) is None):
return(cached.get(arg))
if len(cached) == cache_size:
del cached[next(iter(cached))]
cached.update({arg: func(arg)})
return(cached.get(arg))
return(func_cached)

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

..EE.......FFF..E...
======================================================================
ERROR: test_cache_function_with_vargs (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-1gym7ly/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cached() takes 1 positional argument but 3 were given

======================================================================
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-1gym7ly/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-1gym7ly/solution.py", line 51, in func_cached
    del cached[next(iter(cached))]
StopIteration

======================================================================
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-1gym7ly/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1gym7ly/solution.py", line 34, in zip_with
    iterable_with_min_length = (min(map(len, iterables)))
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-1gym7ly/test.py", line 54, in test_iterate_out_of_order_calls
    self.assertEqual(2 * 'ham', f1('ham'))
AssertionError: 'hamham' != 'hamhamhamhamhamhamhamham'
- hamham
+ hamhamhamhamhamhamhamham


======================================================================
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-1gym7ly/test.py", line 54, in test_iterate_out_of_order_calls
    self.assertEqual(2 * 'ham', f1('ham'))
AssertionError: 'hamham' != 'hamhamhamhamhamhamhamham'
- hamham
+ hamhamhamhamhamhamhamham


======================================================================
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-1gym7ly/test.py", line 54, in test_iterate_out_of_order_calls
    self.assertEqual(2 * 'ham', f1('ham'))
AssertionError: 'hamham' != 'hamhamhamhamhamhamhamham'
- hamham
+ hamhamhamhamhamhamhamham


----------------------------------------------------------------------
Ran 20 tests in 0.013s

FAILED (failures=3, errors=3)

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

Христо обнови решението на 15.03.2013 16:08 (преди над 11 години)

+from itertools import chain
+
+
+def groupby(func, seq):
+ result = {}
+ for elem in seq:
+ if func(elem) in result:
+ result.update({func(elem): result[func(elem)] + [elem]})
+ else:
+ result.update({func(elem): [elem]})
+ return result
+
+
+def compose(func, times):
+ if times == 1:
+ return func
+ return(lambda x: func(compose(func, times - 1)(x)))
+
+
+def iterate(func):
+ first_call = (lambda func: func)
+ yield first_call
+ composition_count = 0
+ while True:
+ composition_count += 1
+ next_call = (lambda *args: (compose(func, composition_count)(*args)))
+ yield(next_call)
+
+
+def zip_with(func, *iterables):
+ if iterables == ():
+ return([])
+ iterable_with_min_length = (min(map(len, iterables)))
+ iterator = 0
+ while iterator != iterable_with_min_length:
+ current = []
+ for item in iterables:
+ current.append(item[iterator])
+ yield(func(*(chain(current))))
+ iterator += 1
+
+
+def cache(func, cache_size):
+ cached = {}
+
+ def func_cached(arg):
+ if not(cached.get(arg) is None):
+ return(cached.get(arg))
+ if len(cached) == cache_size:
+ cached.clear()
+ cached.update({arg: func(arg)})
+ return(cached.get(arg))
+
+ return(func_cached)

Христо обнови решението на 15.03.2013 22:56 (преди над 11 години)

from itertools import chain
+from collections import OrderedDict
def groupby(func, seq):
result = {}
for elem in seq:
if func(elem) in result:
result.update({func(elem): result[func(elem)] + [elem]})
else:
result.update({func(elem): [elem]})
return result
def compose(func, times):
if times == 1:
return func
return(lambda x: func(compose(func, times - 1)(x)))
def iterate(func):
first_call = (lambda func: func)
yield first_call
composition_count = 0
while True:
composition_count += 1
next_call = (lambda *args: (compose(func, composition_count)(*args)))
yield(next_call)
def zip_with(func, *iterables):
if iterables == ():
return([])
iterable_with_min_length = (min(map(len, iterables)))
iterator = 0
while iterator != iterable_with_min_length:
current = []
for item in iterables:
current.append(item[iterator])
yield(func(*(chain(current))))
iterator += 1
def cache(func, cache_size):
- cached = {}
+ cached = OrderedDict()
def func_cached(arg):
if not(cached.get(arg) is None):
return(cached.get(arg))
if len(cached) == cache_size:
- cached.clear()
+ del cached[next(iter(cached))]
cached.update({arg: func(arg)})
return(cached.get(arg))
return(func_cached)