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

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

Към профила на Нели Хатева

Резултати

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

Код

from collections import defaultdict
from collections import deque
def groupby(key_function, sequence):
result = defaultdict(list)
for element in sequence:
key = key_function(element)
result[key].append(element)
return result
def iterate(function):
iterated_function = lambda *args: len(args) == 1 and args[0] or args
while True:
yield iterated_function
iterated_function = \
(lambda f: (lambda *args: function(f(*args))))(iterated_function)
def zip_with(function, *iterables):
if len(iterables) == 0:
return []
minimum_length = min(list(map(len, iterables)))
for i in range(0, minimum_length):
arguments = [x[i] for x in iterables]
result = function(*arguments)
yield result
def cache(function, cache_size):
cache = {}
count_size = 0
queue = deque([], cache_size)
def function_cached(*args):
nonlocal count_size
nonlocal cache_size
if args in cache:
return cache[args]
else:
return_value = function(*args)
if count_size == cache_size:
cache.pop(queue.popleft())
count_size -= 1
cache[args] = return_value
queue.append(args)
count_size += 1
return return_value
return function_cached

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

...E............E...
======================================================================
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-17dost5/test.py", line 104, in test_cache_no_cache
    self.assertEqual(42 * 2, cached_double(42))
  File "/tmp/d20130408-29081-17dost5/solution.py", line 45, in function_cached
    cache.pop(queue.popleft())
IndexError: pop from an empty deque

======================================================================
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-17dost5/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-17dost5/solution.py", line 24, in zip_with
    minimum_length = min(list(map(len, iterables)))
TypeError: object of type 'itertools.repeat' has no len()

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

FAILED (errors=2)

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

Нели обнови решението на 13.03.2013 12:00 (преди около 11 години)

+import itertools
+
+
+def groupby(key_function, sequence):
+ result = {}
+ data = sorted(sequence, key=key_function)
+ grouped = itertools.groupby(data, key_function)
+ for key, group in grouped:
+ result[key] = list(group)
+ return result

Нели обнови решението на 14.03.2013 17:15 (преди около 11 години)

import itertools
def groupby(key_function, sequence):
result = {}
data = sorted(sequence, key=key_function)
grouped = itertools.groupby(data, key_function)
for key, group in grouped:
result[key] = list(group)
return result
+
+
+def iterate(func):
+ pass
+
+
+def zip_with(func, *iterables):
+ list_of_lengths = list(map(len, iterables))
+ if list_of_lengths:
+ minimum_length = min(list_of_lengths)
+ else:
+ minimum_length = 0
+ for i in range(0, minimum_length):
+ arguments = [x[i] for x in iterables]
+ result = func(*arguments)
+ yield result
+
+
+def cache(func, cache_size):
+ pass
+

Нели обнови решението на 15.03.2013 11:25 (преди около 11 години)

-import itertools
+from collections import defaultdict
def groupby(key_function, sequence):
- result = {}
- data = sorted(sequence, key=key_function)
- grouped = itertools.groupby(data, key_function)
- for key, group in grouped:
- result[key] = list(group)
+ result = defaultdict(list)
+ for element in sequence:
+ key = key_function(element)
+ result[key].append(element)
return result
def iterate(func):
pass
def zip_with(func, *iterables):
list_of_lengths = list(map(len, iterables))
if list_of_lengths:
minimum_length = min(list_of_lengths)
else:
minimum_length = 0
for i in range(0, minimum_length):
arguments = [x[i] for x in iterables]
result = func(*arguments)
yield result
def cache(func, cache_size):
pass

Нели обнови решението на 15.03.2013 12:41 (преди около 11 години)

from collections import defaultdict
+from collections import deque
def groupby(key_function, sequence):
result = defaultdict(list)
for element in sequence:
key = key_function(element)
result[key].append(element)
return result
-def iterate(func):
- pass
+def iterate(function):
+ return_value = lambda *args: args
+ while True:
+ yield return_value
+ return_value, = function(return_value)
def zip_with(func, *iterables):
list_of_lengths = list(map(len, iterables))
if list_of_lengths:
minimum_length = min(list_of_lengths)
else:
minimum_length = 0
for i in range(0, minimum_length):
arguments = [x[i] for x in iterables]
result = func(*arguments)
yield result
-def cache(func, cache_size):
- pass
+def cache(function, cache_size):
+ cache = {}
+ queue = deque([], cache_size)
+ count_size = 0
+ def function_cached(*args):
+ nonlocal count_size
+ nonlocal cache_size
+
+ if args in cache:
+ return cache[args]
+ else:
+ return_value = function(*args)
+ if count_size == cache_size:
+ cache.pop(queue.popleft())
+ count_size -= 1
+ cache[args] = return_value
+ queue.append(args)
+ count_size += 1
+ return return_value
+
+ return function_cached

Нели обнови решението на 15.03.2013 16:32 (преди около 11 години)

from collections import defaultdict
from collections import deque
def groupby(key_function, sequence):
result = defaultdict(list)
for element in sequence:
key = key_function(element)
result[key].append(element)
return result
def iterate(function):
- return_value = lambda *args: args
+ iterated_function = lambda *args: len(args) == 1 and args[0] or args
while True:
- yield return_value
- return_value, = function(return_value)
+ yield iterated_function
+ iterated_function = \
+ (lambda f: (lambda *args: function(f(*args))))(iterated_function)
def zip_with(func, *iterables):
list_of_lengths = list(map(len, iterables))
if list_of_lengths:
minimum_length = min(list_of_lengths)
else:
minimum_length = 0
for i in range(0, minimum_length):
arguments = [x[i] for x in iterables]
result = func(*arguments)
yield result
def cache(function, cache_size):
cache = {}
queue = deque([], cache_size)
count_size = 0
def function_cached(*args):
nonlocal count_size
nonlocal cache_size
if args in cache:
return cache[args]
else:
return_value = function(*args)
if count_size == cache_size:
cache.pop(queue.popleft())
count_size -= 1
cache[args] = return_value
queue.append(args)
count_size += 1
return return_value
return function_cached

Нели обнови решението на 15.03.2013 17:22 (преди около 11 години)

from collections import defaultdict
from collections import deque
def groupby(key_function, sequence):
result = defaultdict(list)
for element in sequence:
key = key_function(element)
result[key].append(element)
return result
def iterate(function):
iterated_function = lambda *args: len(args) == 1 and args[0] or args
while True:
yield iterated_function
iterated_function = \
(lambda f: (lambda *args: function(f(*args))))(iterated_function)
-def zip_with(func, *iterables):
- list_of_lengths = list(map(len, iterables))
- if list_of_lengths:
- minimum_length = min(list_of_lengths)
- else:
- minimum_length = 0
+def zip_with(function, *iterables):
+ if len(iterables) == 0:
+ return []
+ minimum_length = min(list(map(len, iterables)))
for i in range(0, minimum_length):
arguments = [x[i] for x in iterables]
- result = func(*arguments)
+ result = function(*arguments)
yield result
def cache(function, cache_size):
cache = {}
- queue = deque([], cache_size)
count_size = 0
+ queue = deque([], cache_size)
def function_cached(*args):
nonlocal count_size
nonlocal cache_size
if args in cache:
return cache[args]
else:
return_value = function(*args)
if count_size == cache_size:
cache.pop(queue.popleft())
count_size -= 1
cache[args] = return_value
queue.append(args)
count_size += 1
return return_value
return function_cached
+