Решение на Четири функции от Димитър Станев

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

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

Резултати

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

Код

def groupby(func, seq):
returndictionary = {}
for item in seq:
result = func(item)
if result in returndictionary.keys():
returndictionary.setdefault(result, []).append(item)
else:
returndictionary.update({result:[item]})
return returndictionary
def iterate(func):
returnlist = range(1,1000)
for i in returnlist:
yield func(func) # doesn't really seem to work
def zip_with(func, *iterables):
for i in range(0, len(iterables[0])):
buffer_list = list()
for j in range (0, len(iterables)):
buffer_list.append(iterables[j][i])
yield func(*buffer_list)
def cache(func, cache_size):
def cached_func(x):
cached_list = list()
result = func(x)
if result not in cached_list:
cached_list.append(result)
else:
cached_list.pop()
cached_list.append(result)
print(result)
return cached_func

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

F
Stdout:
84
F
Stdout:
256
EF
Stdout:
84
F
Stdout:
2
.....EEEEEEE...
======================================================================
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-1cc4fdt/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: cached_func() takes 1 positional argument but 3 were given

======================================================================
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-1cc4fdt/test.py", line 38, in test_iterate_ordered_calls
    f = next(powers_of_two)
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 14, in iterate
    yield func(func) # doesn't really seem to work
  File "/tmp/d20130408-29081-1cc4fdt/test.py", line 37, in <lambda>
    powers_of_two = solution.iterate(lambda x: x*2)
TypeError: unsupported operand type(s) for *: 'function' and 'int'

======================================================================
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-1cc4fdt/test.py", line 49, in test_iterate_out_of_order_calls
    f0 = next(powers_of_two)
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 14, in iterate
    yield func(func) # doesn't really seem to work
  File "/tmp/d20130408-29081-1cc4fdt/test.py", line 48, in <lambda>
    powers_of_two = solution.iterate(lambda x: x*2)
TypeError: unsupported operand type(s) for *: 'function' and 'int'

======================================================================
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-1cc4fdt/test.py", line 49, in test_iterate_out_of_order_calls
    f0 = next(powers_of_two)
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 14, in iterate
    yield func(func) # doesn't really seem to work
  File "/tmp/d20130408-29081-1cc4fdt/test.py", line 48, in <lambda>
    powers_of_two = solution.iterate(lambda x: x*2)
TypeError: unsupported operand type(s) for *: 'function' and 'int'

======================================================================
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-1cc4fdt/test.py", line 49, in test_iterate_out_of_order_calls
    f0 = next(powers_of_two)
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 14, in iterate
    yield func(func) # doesn't really seem to work
  File "/tmp/d20130408-29081-1cc4fdt/test.py", line 48, in <lambda>
    powers_of_two = solution.iterate(lambda x: x*2)
TypeError: unsupported operand type(s) for *: 'function' and 'int'

======================================================================
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-1cc4fdt/test.py", line 33, in test_iterate_start_with_identity_function
    no_brackets = next(bracketisers)
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 14, in iterate
    yield func(func) # doesn't really seem to work
  File "/tmp/d20130408-29081-1cc4fdt/test.py", line 32, in <lambda>
    bracketisers = solution.iterate(lambda x: '(' + x + ')') # there's no such word, really
TypeError: Can't convert 'function' object to str implicitly

======================================================================
ERROR: 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-1cc4fdt/test.py", line 67, in test_zip_with_empty
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 17, in zip_with
    for i in range(0, len(iterables[0])):
IndexError: tuple index out of range

======================================================================
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-1cc4fdt/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1cc4fdt/solution.py", line 20, in zip_with
    buffer_list.append(iterables[j][i])
TypeError: 'itertools.repeat' object is not subscriptable

======================================================================
FAIL: 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-1cc4fdt/test.py", line 131, in test_cache_cache_is_not_global
    self.assertEqual(42 * 2, cached_double1(42))
AssertionError: 84 != None

Stdout:
84

======================================================================
FAIL: 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-1cc4fdt/test.py", line 117, in test_cache_call_is_cached
    self.assertEqual(256, cached_double(128))
AssertionError: 256 != None

Stdout:
256

======================================================================
FAIL: 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-1cc4fdt/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
AssertionError: 84 != None

Stdout:
84

======================================================================
FAIL: 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-1cc4fdt/test.py", line 144, in test_cache_size_is_respected
    self.assertEqual(2, cached_double(1))
AssertionError: 2 != None

Stdout:
2

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

FAILED (failures=4, errors=8)

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

Димитър обнови решението на 15.03.2013 21:48 (преди над 11 години)

+def groupby(func, seq):
+ returndictionary = {}
+ for item in seq:
+ result = func(item)
+ if result in returndictionary.keys():
+ returndictionary.setdefault(result, []).append(item)
+ else:
+ returndictionary.update({result:[item]})
+ return returndictionary
+
+def iterate(func):
+ returnlist = range(1,1000)
+ for i in returnlist:
+ yield func(func) # doesn't really seem to work
+
+def zip_with(func, *iterables):
+ for i in range(0, len(iterables[0])):
+ buffer_list = list()
+ for j in range (0, len(iterables)):
+ buffer_list.append(iterables[j][i])
+ yield func(*buffer_list)
+
+def cache(func, cache_size):
+ def cached_func(x):
+ cached_list = list()
+ result = func(x)
+ if result not in cached_list:
+ cached_list.append(result)
+ else:
+ cached_list.pop()
+ cached_list.append(result)
+ print(result)
+ return cached_func
+