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

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

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

Резултати

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

Код

"""groupby"""
def groupby(func, seq):
my_dict = dict()
for x in seq:
result = func(x)
if result in my_dict.keys():
my_dict[result].append(x)
else:
my_dict[result] = [x, ]
return my_dict
"""iterate"""
def combine_funcs(func_one, func_two):
def composed(*args):
return func_one(func_two(*args))
return composed
def iterate(func):
def identity(input):
return input
while True:
yield identity
identity = combine_funcs(func, identity)
"""zip_with"""
def zip_with(func, *iterables):
if len(iterables) == 0:
iterables = []
return None
min_len = 10000
for x in iterables:
if min_len > len(x):
min_len = len(x)
for i in range(0, min_len):
params = list()
for j in iterables:
params.append(j[i])
yield func(*params)
"""cache"""
def cache(func, cache_size):
cached_results = [[], []]
def func_cached(unique_number):
while True:
if unique_number in cached_results[0]:
too_long_line = cached_results[0].index(unique_number)
return cached_results[1][too_long_line]
else:
result_func = func(unique_number)
cached_results[0].append(unique_number)
cached_results[1].append(result_func)
if len(cached_results[0]) > cache_size:
del cached_results[0][0]
del cached_results[1][0]
return result_func
return func_cached

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

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

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

FAILED (errors=2)

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

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

+"""groupby"""
+def groupby(func, seq):
+ my_dict = dict()
+ for x in seq:
+ result = func(x)
+ if result in my_dict.keys():
+ my_dict[result].append(x)
+ else:
+ my_dict[result] = [x, ]
+ return my_dict
+
+"""iterate"""
+def combine_funcs(func_one, func_two):
+ def composed(*args):
+ return func_one(func_two(*args))
+ return composed
+
+
+def iterate(func):
+ def identity(input):
+ return input
+
+ while True:
+ yield identity
+ identity = combine_funcs(func, identity)
+
+"""zip_with"""
+def zip_with(func, *iterables):
+ if len(iterables) == 0:
+ iterables = []
+ return None
+ min_len = 10000
+ for x in iterables:
+ if min_len > len(x):
+ min_len = len(x)
+ for i in range(0, min_len):
+ params = list()
+ for j in iterables:
+ params.append(j[i])
+ yield func(*params)
+
+"""cache"""
+def cache(func, cache_size):
+ cached_results = [[], []]
+
+ def func_cached(unique_number):
+ while True:
+ if unique_number in cached_results[0]:
+ too_long_line = cached_results[0].index(unique_number)
+ return cached_results[1][too_long_line]
+ else:
+ result_func = func(unique_number)
+ cached_results[0].append(unique_number)
+ cached_results[1].append(result_func)
+ if len(cached_results[0]) > cache_size:
+ del cached_results[0][0]
+ del cached_results[1][0]
+ return result_func
+
+ return func_cached