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

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

Към профила на Емил Гелев

Резултати

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

Код

#groupby
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if result.__contains__(key):
result[key].append(element)
else:
result[key] = [element]
return result
#iterate
from itertools import count
list_ = ['dummy']
def set_list(list_, func):
old_func = list_[0]
new_func = lambda *arg: func(old_func(*arg))
list_[0] = new_func
return old_func
def iterate(func):
global list_
list_[0] = lambda x: x
return (set_list(list_,func) for i in count(0))
#zip_with
def zip_with(func, *iterables):
if (len(iterables) != 0):
min_len = min(map(len, iterables))
result = []
for i in range(min_len):
list_ = []
for element in iterables:
list_.append(element[i])
arg = tuple(list_)
result.append(func(*arg))
return result
else:
return []
#cache
def cache(func, cache_size):
def func_cached(x, l=[]):
dictionary = dict(l)
if x in dictionary:
return dictionary[x]
else:
value = func(x)
l.append((x, value))
if len(l) >= cache_size + 1:
l.pop(0)
return value
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-12ef4ma/test.py", line 160, in test_cache_function_with_vargs
    self.assertEqual(6, cached_sum(1, 2, 3))
TypeError: func_cached() takes from 1 to 2 positional arguments 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-12ef4ma/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-12ef4ma/solution.py", line 34, in zip_with
    min_len = min(map(len, iterables))
TypeError: object of type 'itertools.repeat' has no len()

----------------------------------------------------------------------
Ran 20 tests in 0.011s

FAILED (errors=2)

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

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

+#groupby
+
+def groupby(func, seq):
+ result = {}
+ for element in seq:
+ key = func(element)
+ if result.__contains__(key):
+ result[key].append(element)
+ else:
+ result[key] = [element]
+ return result
+
+
+#iterate
+
+from itertools import count
+
+l = ['dummy']
+
+def set_list(l, func):
+ old_func = l[0]
+ new_func = lambda *arg: func(old_func(*arg))
+ l[0] = new_func
+ return old_func
+
+def iterate(func):
+ global l
+ l[0] = lambda x: x
+ return (set_list(l,func) for i in count(0))
+
+#zip_with
+
+def zip_with(func, *iterables):
+ if (len(iterables) != 0 ):
+ min_len = min(map(len, iterables))
+ result = []
+ for i in range(min_len):
+ list_ = []
+ for element in iterables:
+ list_.append(element[i])
+ arg = tuple(list_)
+ result.append(func(*arg))
+ return result
+ else:
+ return []
+
+#cache
+
+def cache(func, cache_size):
+ def func_cached(x, l=[]):
+ dictionary = dict(l)
+ if x in dictionary:
+ return dictionary[x]
+ else:
+ value = func(x)
+ l.append((x,value))
+ if len(l) >= cache_size + 1:
+ l.pop(0)
+ return value
+ return func_cached
+
+
+

Емил обнови решението на 15.03.2013 21:38 (преди над 11 години)

#groupby
def groupby(func, seq):
result = {}
for element in seq:
key = func(element)
if result.__contains__(key):
result[key].append(element)
else:
result[key] = [element]
return result
-
#iterate
from itertools import count
-l = ['dummy']
+list_ = ['dummy']
-def set_list(l, func):
- old_func = l[0]
+def set_list(list_, func):
+ old_func = list_[0]
new_func = lambda *arg: func(old_func(*arg))
- l[0] = new_func
+ list_[0] = new_func
return old_func
def iterate(func):
- global l
- l[0] = lambda x: x
- return (set_list(l,func) for i in count(0))
+ global list_
+ list_[0] = lambda x: x
+ return (set_list(list_,func) for i in count(0))
#zip_with
def zip_with(func, *iterables):
- if (len(iterables) != 0 ):
+ if (len(iterables) != 0):
min_len = min(map(len, iterables))
result = []
for i in range(min_len):
list_ = []
for element in iterables:
list_.append(element[i])
arg = tuple(list_)
result.append(func(*arg))
return result
else:
return []
#cache
def cache(func, cache_size):
def func_cached(x, l=[]):
dictionary = dict(l)
if x in dictionary:
return dictionary[x]
else:
value = func(x)
- l.append((x,value))
+ l.append((x, value))
if len(l) >= cache_size + 1:
l.pop(0)
return value
- return func_cached
-
+ return func_cached
-
-