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

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

Към профила на Александър Цветанов

Резултати

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

Код

def groupby(func, seq):
group = {}
group_odd = [x for x in seq if not func(x)]
group_even = [x for x in seq if func(x)]
group [0] = group_odd
group [1] = group_even
return group
def iterate(func):
while 1:
yield func
def zip_with(func, *iterables):
i = 0
if len(iterables) == 0:
yield 0
else:
for i in iterables:
result = map(func, *iterables)
yield list(result)

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

EEEEEFF.FFFFFFFEF.FF
======================================================================
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-1e643o7/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-1e643o7/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-1e643o7/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-1e643o7/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-1e643o7/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_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-1e643o7/test.py", line 67, in test_zip_with_empty
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-1e643o7/solution.py", line 18, in zip_with
    yield list(result)
UnboundLocalError: local variable 'result' referenced before assignment

======================================================================
FAIL: test_groupby_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-1e643o7/test.py", line 10, in test_groupby_empty
    self.assertEqual(expected, actual)
AssertionError: {} != {0: [], 1: []}
- {}
+ {0: [], 1: []}

======================================================================
FAIL: test_groupby_no_repetitions (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-1e643o7/test.py", line 15, in test_groupby_no_repetitions
    self.assertEqual(expected, actual)
AssertionError: {1: [1], 2: [2], 3: [3], 5: [5], 'se7en': ['se7en']} != {0: [], 1: [1, 2, 3, 5, 'se7en']}
- {1: [1], 2: [2], 3: [3], 5: [5], 'se7en': ['se7en']}
+ {0: [], 1: [1, 2, 3, 5, 'se7en']}

======================================================================
FAIL: test_groupby_simple_types (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-1e643o7/test.py", line 20, in test_groupby_simple_types
    self.assertEqual(expected, actual)
AssertionError: {'even': [2, 8, 10, 12], 'odd': [1, 3, 5, 9]} != {0: [], 1: [1, 2, 3, 5, 8, 9, 10, 12]}
- {'even': [2, 8, 10, 12], 'odd': [1, 3, 5, 9]}
+ {0: [], 1: [1, 2, 3, 5, 8, 9, 10, 12]}

======================================================================
FAIL: test_groupby_with_generator (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-1e643o7/test.py", line 25, in test_groupby_with_generator
    self.assertEqual(expected, actual)
AssertionError: {'small': [0, 5, 10, 15, 20, 25, 30], 'big': [35, 40, 45]} != {0: [], 1: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]}
- {'big': [35, 40, 45], 'small': [0, 5, 10, 15, 20, 25, 30]}
+ {0: [], 1: [0, 5, 10, 15, 20, 25, 30, 35, 40, 45]}

======================================================================
FAIL: 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-1e643o7/test.py", line 39, in test_iterate_ordered_calls
    self.assertEqual(1 * 'eggs', f('eggs'))
AssertionError: 'eggs' != 'eggseggs'
- eggs
+ eggseggs


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


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


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


======================================================================
FAIL: 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-1e643o7/test.py", line 34, in test_iterate_start_with_identity_function
    self.assertEqual('hello world', no_brackets('hello world'))
AssertionError: 'hello world' != '(hello world)'
- hello world
+ (hello world)
? +           +


======================================================================
FAIL: 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-1e643o7/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
AssertionError: Lists differ: ['John Coltrane', 'Miles Davis... != [['John Coltrane', 'Miles Davi...

First differing element 0:
John Coltrane
['John Coltrane', 'Miles Davis']

First list contains 1 additional elements.
First extra element 1:
Miles Davis

- ['John Coltrane', 'Miles Davis']
+ [['John Coltrane', 'Miles Davis']]
? +                                +


======================================================================
FAIL: test_zip_with_one_shorter_seqence (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-1e643o7/test.py", line 74, in test_zip_with_one_shorter_seqence
    self.assertEqual(expected, list(actual))
AssertionError: Lists differ: ['CharlieParker', 'DizzyGilles... != [['CharlieParker', 'DizzyGille...

First differing element 0:
CharlieParker
['CharlieParker', 'DizzyGillespie']

First list contains 1 additional elements.
First extra element 1:
DizzyGillespie

- ['CharlieParker', 'DizzyGillespie']
+ [['CharlieParker', 'DizzyGillespie']]
? +                                   +


======================================================================
FAIL: test_zip_with_vargs_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-1e643o7/test.py", line 87, in test_zip_with_vargs_function
    self.assertEqual(expected, list(actual))
AssertionError: Lists differ: [19] != [[19]]

First differing element 0:
19
[19]

- [19]
+ [[19]]
? +    +


----------------------------------------------------------------------
Ran 20 tests in 0.022s

FAILED (failures=12, errors=6)

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

Александър обнови решението на 15.03.2013 19:19 (преди около 11 години)

+def groupby(func, seq):
+ group = {}
+ group_odd = [x for x in seq if not func(x)]
+ group_even = [x for x in seq if func(x)]
+ group [0] = group_odd
+ group [1] = group_even
+ return group
+def iterate(func):
+ while 1:
+ yield func
+def zip_with(func, *iterables):
+ i = 0
+ if len(iterables) == 0:
+ yield 0
+ else:
+ for i in iterables:
+ result = map(func, *iterables)
+ yield list(result)