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

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

Към профила на Станимир Николов

Резултати

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

Код

#first
def groupby(func, seq):
keys = {func(x) for x in seq}
result = {x: [] for x in keys}
for key in keys:
result[key] = [y for y in seq if key == func(y)]
return result
#second
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def composition(*args, **kwargs):
return func_1(func_2(*args, **kwargs))
return composition
def iterate(func):
curent_result = (lambda x: x)
yield curent_result
while True:
curent_result = compose(curent_result, func)
yield curent_result
#third
def zip_with(func, *iterables):
if len(iterables) == 0:
while True:
yield func
else:
current_index = 0
while True:
params = []
for x in iterables:
if(len(x) > current_index):
params.append(x[current_index])
else:
break
if(len(params) < len(iterables)):
break
yield func(*params)
current_index += 1

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

EEEEE..........EE...
======================================================================
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-14cw2xc/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-14cw2xc/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-14cw2xc/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-14cw2xc/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-14cw2xc/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 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
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-14cw2xc/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-14cw2xc/solution.py", line 54, in zip_with
    if(len(x) > current_index):
TypeError: object of type 'itertools.repeat' has no len()

----------------------------------------------------------------------
Ran 20 tests in 2.139s

FAILED (errors=7)

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

Станимир обнови решението на 15.03.2013 11:00 (преди над 11 години)

+def groupby(func, seq):
+ keys = {func(x) for x in seq}
+ result = {x: [] for x in keys}
+ for key in keys:
+ result[key] = [y for y in seq if key == func(y)]
+ return result

Станимир обнови решението на 15.03.2013 22:49 (преди над 11 години)

def groupby(func, seq):
keys = {func(x) for x in seq}
result = {x: [] for x in keys}
for key in keys:
result[key] = [y for y in seq if key == func(y)]
return result
+
+def compose(func_1, func_2, unpack=False):
+ """
+ compose(func_1, func_2, unpack=False) -> function
+
+ The function returned by compose is a composition of func_1 and func_2.
+ That is, compose(func_1, func_2)(5) == func_1(func_2(5))
+ """
+ if not callable(func_1):
+ raise TypeError("First argument to compose must be callable")
+ if not callable(func_2):
+ raise TypeError("Second argument to compose must be callable")
+
+ if unpack:
+ def composition(*args, **kwargs):
+ return func_1(*func_2(*args, **kwargs))
+ else:
+ def composition(*args, **kwargs):
+ return func_1(func_2(*args, **kwargs))
+ return composition
+
+def iterate(func):
+ curent_result = (lambda x: x)
+ yield curent_result
+ while True:
+ curent_result = compose(curent_result, func)
+ yield curent_result
+
+

Станимир обнови решението на 15.03.2013 23:25 (преди над 11 години)

+#first
+
+
def groupby(func, seq):
keys = {func(x) for x in seq}
result = {x: [] for x in keys}
for key in keys:
- result[key] = [y for y in seq if key == func(y)]
+ result[key] = [y for y in seq if key == func(y)]
return result
+#second
+
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def composition(*args, **kwargs):
return func_1(func_2(*args, **kwargs))
return composition
+
def iterate(func):
curent_result = (lambda x: x)
yield curent_result
while True:
- curent_result = compose(curent_result, func)
+ curent_result = compose(curent_result, func)
yield curent_result
-
-

Станимир обнови решението на 15.03.2013 23:52 (преди над 11 години)

#first
def groupby(func, seq):
keys = {func(x) for x in seq}
result = {x: [] for x in keys}
for key in keys:
result[key] = [y for y in seq if key == func(y)]
return result
#second
def compose(func_1, func_2, unpack=False):
"""
compose(func_1, func_2, unpack=False) -> function
The function returned by compose is a composition of func_1 and func_2.
That is, compose(func_1, func_2)(5) == func_1(func_2(5))
"""
if not callable(func_1):
raise TypeError("First argument to compose must be callable")
if not callable(func_2):
raise TypeError("Second argument to compose must be callable")
if unpack:
def composition(*args, **kwargs):
return func_1(*func_2(*args, **kwargs))
else:
def composition(*args, **kwargs):
return func_1(func_2(*args, **kwargs))
return composition
def iterate(func):
curent_result = (lambda x: x)
yield curent_result
while True:
curent_result = compose(curent_result, func)
yield curent_result
+
+
+#third
+
+
+def zip_with(func, *iterables):
+ if len(iterables) == 0:
+ while True:
+ yield func
+ else:
+ current_index = 0
+ while True:
+ params = []
+ for x in iterables:
+ if(len(x) > current_index):
+ params.append(x[current_index])
+ else:
+ break
+ if(len(params) < len(iterables)):
+ break
+ yield func(*params)
+ current_index += 1

Айде за спорта да сложа и вижданията ми за 4-тата функция.

fourth

cache_dict = {} cache_keys = []

def help_with_cache(x, func, cache_size): for key, value in cache_dict.items(): if key == x: return value result = func(x) cache_dict[x] = result cache_keys.append(x)

if cache_size < len(cache_keys):
    removed_element_key = cache_keys[0]
    cache_keys.pop(0)
    del cache_dict[removed_element_key]
return result

def cache(func, cache_size): return lambda x: help_with_cache(x, func, cache_size)