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

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

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

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 19 успешни тест(а)
  • 1 неуспешни тест(а)

Код

from collections import OrderedDict
def groupby(func, seq):
dict_result = {}
functionality_set = set(func(seq_element) for seq_element in seq)
for functionality_value in functionality_set:
dict_result[functionality_value] = [
seq_element for seq_element
in seq if func(seq_element) == functionality_value]
return dict_result
def iterate(func):
func_iterator = lambda iterator: lambda x: func(iterator(x))
result_function = lambda x: x
yield result_function
while True:
result_function = func_iterator(result_function)
yield result_function
def zip_with(func, *iterables):
def minimal_element(*iters):
if len(list(*iters)) > 0:
return min(list(*iters))
else:
return 0
for index_of_iterable in range(0, minimal_element([len(list(iterable))
for iterable in iterables])):
yield func(*[iterable[index_of_iterable] for iterable in iterables])
def cache(func, cache_size):
memory = OrderedDict()
def func_cached(*args):
if cache_size < len(memory):
memory.popitem(0)
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return func_cached

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

................E...
======================================================================
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-vr54tx/test.py", line 82, in test_zip_with_infinite_sequence
    self.assertEqual(expected, list(actual))
  File "/tmp/d20130408-29081-vr54tx/solution.py", line 32, in zip_with
    for iterable in iterables])):
  File "/tmp/d20130408-29081-vr54tx/solution.py", line 32, in <listcomp>
    for iterable in iterables])):
MemoryError

----------------------------------------------------------------------
Ran 20 tests in 5.091s

FAILED (errors=1)

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

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

+def groupby(func, seq):
+ dict_result = {}
+ functionality_set = set(func(seq_element) for seq_element in seq)
+
+ for functionality_value in functionality_set:
+ dict_result[functionality_value] = [
+ seq_element for seq_element
+ in seq if func(seq_element) == functionality_value]
+ return dict_result
+
+
+def iterate(func):
+ func_iterator = lambda iterator: lambda x: func(iterator(x))
+ result_function = lambda x: x
+ yield result_function
+
+ while True:
+ result_function = func_iterator(f)
+ yield result_function
+
+
+def zip_with(func, *iterables):
+ for index_of_iterable in range(0, min([len(iterable)
+ for iterable in iterables])):
+ yield func(*[iterable[index_of_iterable] for iterable in iterables])
+
+
+def cache(func, cache_size):
+ memory = OrderedDict()
+
+ def func_cached(*args):
+ if cache_size < len(memory):
+ memory.popitem(0)
+ if args in memory:
+ return memory[args]
+ result = func(*args)
+ memory[args] = result
+ return result
+ return func_cached

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

def groupby(func, seq):
dict_result = {}
functionality_set = set(func(seq_element) for seq_element in seq)
for functionality_value in functionality_set:
dict_result[functionality_value] = [
seq_element for seq_element
in seq if func(seq_element) == functionality_value]
return dict_result
def iterate(func):
func_iterator = lambda iterator: lambda x: func(iterator(x))
result_function = lambda x: x
yield result_function
while True:
- result_function = func_iterator(f)
+ result_function = func_iterator(result_function)
yield result_function
def zip_with(func, *iterables):
for index_of_iterable in range(0, min([len(iterable)
for iterable in iterables])):
yield func(*[iterable[index_of_iterable] for iterable in iterables])
def cache(func, cache_size):
memory = OrderedDict()
def func_cached(*args):
if cache_size < len(memory):
memory.popitem(0)
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return func_cached

min го изпълнявам в случая където го използвам, върху списък от цели числа, което мисля че е ок ... стига да не е празен списъка. А len ... мамка му прав си и честно казано доста трябва да помисля за алтернатива, защото засега не се сещам.

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

+from collections import OrderedDict
+
def groupby(func, seq):
dict_result = {}
functionality_set = set(func(seq_element) for seq_element in seq)
for functionality_value in functionality_set:
dict_result[functionality_value] = [
seq_element for seq_element
in seq if func(seq_element) == functionality_value]
return dict_result
def iterate(func):
func_iterator = lambda iterator: lambda x: func(iterator(x))
result_function = lambda x: x
yield result_function
while True:
result_function = func_iterator(result_function)
yield result_function
def zip_with(func, *iterables):
for index_of_iterable in range(0, min([len(iterable)
for iterable in iterables])):
yield func(*[iterable[index_of_iterable] for iterable in iterables])
def cache(func, cache_size):
memory = OrderedDict()
def func_cached(*args):
if cache_size < len(memory):
memory.popitem(0)
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return func_cached

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

from collections import OrderedDict
+
def groupby(func, seq):
dict_result = {}
functionality_set = set(func(seq_element) for seq_element in seq)
for functionality_value in functionality_set:
dict_result[functionality_value] = [
seq_element for seq_element
in seq if func(seq_element) == functionality_value]
return dict_result
def iterate(func):
func_iterator = lambda iterator: lambda x: func(iterator(x))
result_function = lambda x: x
yield result_function
while True:
result_function = func_iterator(result_function)
yield result_function
def zip_with(func, *iterables):
- for index_of_iterable in range(0, min([len(iterable)
+ def minimal_element(*iters):
+ if len(list(*iters)) > 0:
+ return min(list(*iters))
+ else:
+ return 0
+ for index_of_iterable in range(0, minimal_element([len(list(iterable))
for iterable in iterables])):
yield func(*[iterable[index_of_iterable] for iterable in iterables])
def cache(func, cache_size):
memory = OrderedDict()
def func_cached(*args):
if cache_size < len(memory):
memory.popitem(0)
if args in memory:
return memory[args]
result = func(*args)
memory[args] = result
return result
return func_cached