Решение на Четири функции от Добринка Табакова

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

Към профила на Добринка Табакова

Резултати

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

Код

def groupby(f, seq):
return {f(x): [i for i in seq if f(i) == f(x)] for x in seq}
def zip_with(f, *iterables):
if iterables == ():
return []
min_len = min([len(it) for it in iterables])
print(min_len)
for i in range(min_len):
yield f(*[it[i] for it in iterables])

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

EEEEE.....EEEEE.E...
======================================================================
ERROR: test_cache_cache_is_not_global (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-wlepjs/test.py", line 129, in test_cache_cache_is_not_global
    cached_double1 = solution.cache(double, 3)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: test_cache_call_is_cached (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-wlepjs/test.py", line 116, in test_cache_call_is_cached
    cached_double = solution.cache(double, 10)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
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-wlepjs/test.py", line 159, in test_cache_function_with_vargs
    cached_sum = solution.cache(sum_varargs, 10)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
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-wlepjs/test.py", line 103, in test_cache_no_cache
    cached_double = solution.cache(double, 0)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: test_cache_size_is_respected (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-wlepjs/test.py", line 143, in test_cache_size_is_respected
    cached_double = solution.cache(double, 2)
AttributeError: 'module' object has no attribute 'cache'

======================================================================
ERROR: test_iterate_ordered_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-wlepjs/test.py", line 37, in test_iterate_ordered_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: 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-wlepjs/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: 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-wlepjs/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: 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-wlepjs/test.py", line 48, in test_iterate_out_of_order_calls
    powers_of_two = solution.iterate(lambda x: x*2)
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
ERROR: test_iterate_start_with_identity_function (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-wlepjs/test.py", line 32, in test_iterate_start_with_identity_function
    bracketisers = solution.iterate(lambda x: '(' + x + ')') # there's no such word, really
AttributeError: 'module' object has no attribute 'iterate'

======================================================================
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-wlepjs/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-wlepjs/solution.py", line 7, in zip_with
    min_len = min([len(it) for it in iterables])
  File "/tmp/d20130408-29081-wlepjs/solution.py", line 7, in <listcomp>
    min_len = min([len(it) for it in iterables])
TypeError: object of type 'itertools.repeat' has no len()

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

FAILED (errors=11)

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

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

+def groupby(f, seq):
+ return {f(x): [i for i in seq if f(i) == f(x)] for x in seq}
+
+def zip_with(f, *iterables):
+ if iterables == ():
+ return []
+ min_len = min([len(it) for it in iterables])
+ print(min_len)
+ for i in range(min_len):
+ yield f(*[it[i] for it in iterables])

Ето последната, която не можах да предам, ако има някакво значение...

def cache(f, n): if not hasattr(cache, "cache"): cache.cache = [] def func_cached(*args): for i in cache.cache: if i[0] == args: return i[1]

    if len(cache.cache) >= n:
        cache.cache.pop(0)
    cache.cache.append((args, f(*args)))
    return f(*args)

return func_cached