Решение на Четири функции от Христо Мохамед

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

Към профила на Христо Мохамед

Резултати

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

Код

import inspect
import collections
import functools
def groupby(func, seq):
returned_dict = {}
for item in seq:
key_to_test = func(item)
if key_to_test in returned_dict:
returned_dict[key_to_test].append(item)
else:
returned_dict[key_to_test] = [item]
return returned_dict
def iterate(fn):
def repeater(arg, _count=1):
for i in range(_count):
arg = fn(arg)
return arg
count = 0
while True:
yield functools.partial(repeater, _count=count)
count += 1
def zip_with(func, *iterables):
if len(iterables) == 0:
def generator():
yield None
else:
variable_length = len(iterables)
shortest_variable_length = len(iterables[0])
for item in iterables:
if shortest_variable_length > len(item):
shortest_variable_length = len(item)
def generator(variable_length, iterables, shortest_variable_length):
argument_list = []
second_position = 0
for first_position in range(0, variable_length):
argument_list.append(
iterables[first_position][second_position])
while second_position < shortest_variable_length:
yield func(*argument_list)
second_position += 1
argument_list = []
for first_position in range(0, variable_length):
argument_list.append(
iterables[first_position][second_position])
generate_it = generator(
variable_length,
iterables,
shortest_variable_length)
return_list = []
for items in range(0, shortest_variable_length):
return_list.append(next(generate_it))
return return_list
def cache(func, cache_size):
cache.cache_dict = collections.OrderedDict()
cache.cache_size = cache_size
cache.current_size = 0
def func_cached(value):
if cache.current_size == 0 and cache.cache_size == 0:
return func(value)
if cache.current_size == 0 and cache.cache_size > 0:
cache.cache_dict[value] = func(value)
cache.current_size += 1
return cache.cache_dict[value]
flag = False
for key in cache.cache_dict:
if key == value:
flag = True
break
if flag:
return cache.cache_dict[value]
else:
if cache.cache_size <= cache.current_size:
trash = cache.cache_dict.popitem(last=False)
cache.cache_dict[value] = func(value)
trash = cache.cache_dict.move_to_end(value, last=True)
cache.current_size += 1
return cache.cache_dict[value]
else:
cache.cache_dict[value] = func(value)
cache.current_size += 1
return cache.cache_dict[value]
return func_cached

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

F.E............EE...
======================================================================
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-zj7jrv/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_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-zj7jrv/test.py", line 66, in test_zip_with_empty
    actual = solution.zip_with(lambda x: x)
  File "/tmp/d20130408-29081-zj7jrv/solution.py", line 60, in zip_with
    return return_list
UnboundLocalError: local variable 'return_list' referenced before assignment

======================================================================
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-zj7jrv/test.py", line 81, in test_zip_with_infinite_sequence
    actual = solution.zip_with(lambda x, y, z: x + y + z, first_names, spaces, last_names)
  File "/tmp/d20130408-29081-zj7jrv/solution.py", line 36, in zip_with
    if shortest_variable_length > len(item):
TypeError: object of type 'itertools.repeat' has no len()

======================================================================
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-zj7jrv/test.py", line 133, in test_cache_cache_is_not_global
    self.assertEqual(2, call_count)
AssertionError: 2 != 1

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

FAILED (failures=1, errors=3)

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

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

+import inspect
+import collections
+import functools
+
+
+def groupby(func, seq):
+ returned_dict = {}
+ for item in seq:
+ key_to_test = func(item)
+ if key_to_test in returned_dict:
+ returned_dict[key_to_test].append(item)
+ else:
+ returned_dict[key_to_test] = [item]
+ return returned_dict
+
+
+def iterate(fn):
+ def repeater(arg, _count=1):
+ for i in range(_count):
+ arg = fn(arg)
+ return arg
+ count = 0
+ while True:
+ yield functools.partial(repeater, _count=count)
+ count += 1
+
+
+def zip_with(func, *iterables):
+ if len(iterables) == 0:
+ def generator():
+ yield None
+ else:
+ variable_length = len(iterables)
+ shortest_variable_length = len(iterables[0])
+ for item in iterables:
+ if shortest_variable_length > len(item):
+ shortest_variable_length = len(item)
+
+ def generator(variable_length, iterables, shortest_variable_length):
+
+ argument_list = []
+ second_position = 0
+ for first_position in range(0, variable_length):
+ argument_list.append(
+ iterables[first_position][second_position])
+ while second_position < shortest_variable_length:
+ yield func(*argument_list)
+ second_position += 1
+ argument_list = []
+ for first_position in range(0, variable_length):
+ argument_list.append(
+ iterables[first_position][second_position])
+ generate_it = generator(
+ variable_length,
+ iterables,
+ shortest_variable_length)
+ return_list = []
+ for items in range(0, shortest_variable_length):
+ return_list.append(next(generate_it))
+ return return_list
+
+
+def cache(func, cache_size):
+ cache.cache_dict = collections.OrderedDict()
+ cache.cache_size = cache_size
+ cache.current_size = 0
+
+ def func_cached(value):
+ if cache.current_size == 0 and cache.cache_size == 0:
+ return func(value)
+ if cache.current_size == 0 and cache.cache_size > 0:
+ cache.cache_dict[value] = func(value)
+ cache.current_size += 1
+ return cache.cache_dict[value]
+ flag = False
+ for key in cache.cache_dict:
+ if key == value:
+ flag = True
+ break
+ if flag:
+ return cache.cache_dict[value]
+ else:
+ if cache.cache_size <= cache.current_size:
+ trash = cache.cache_dict.popitem(last=False)
+ cache.cache_dict[value] = func(value)
+ trash = cache.cache_dict.move_to_end(value, last=True)
+ cache.current_size += 1
+ return cache.cache_dict[value]
+ else:
+ cache.cache_dict[value] = func(value)
+ cache.current_size += 1
+ return cache.cache_dict[value]
+ return func_cached