Решение на Генератори и итератори от Валентин Латунов

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

Към профила на Валентин Латунов

Резултати

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

Код

def fibonacci():
num1, num2 = 1, 0
while True:
yield num1
num1, num2 = num1 + num2, num1
def primes():
to_yield = 2
prev_primes = [2]
while True:
limit = to_yield ** 0.5
for prime in prev_primes:
if prime > limit:
prev_primes.append(to_yield)
yield to_yield
break
if to_yield % prime == 0:
break
to_yield += 1
def alphabet(*, code=None, letters=None):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
if letters:
return iter(letters)
if code:
return iter(alphabets[code])
def intertwined_sequences(iter_obj, *, generator_definitions={}):
using_funcs = []
returned_generators = []
for vector in iter_obj:
passed_args = {
key: val for key, val in vector.items() if key != 'length'
and key != 'sequence'
}
func = vector['sequence']
if func not in using_funcs:
if func in generator_definitions:
returned_generators.append(
generator_definitions[func](**passed_args)
)
else:
returned_generators.append(globals()[func](**passed_args))
else:
returned_generators.append(
returned_generators[using_funcs.index(func)]
)
using_funcs.append(func)
for _ in range(vector['length']):
yield next(returned_generators[-1])

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

......EE......
======================================================================
ERROR: test_generator_definitions (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_infinite_intertwined (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 4.682s

FAILED (errors=2)

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

Валентин обнови решението на 02.04.2015 21:59 (преди над 9 години)

+def fibonacci():
+ num1, num2 = 1, 0
+ while True:
+ yield num1
+ num1, num2 = num1 + num2, num1
+
+
+def primes():
+ to_yield = 2
+ prev_primes = [2]
+ while True:
+ limit = to_yield ** 0.5
+ for prime in prev_primes:
+ if prime > limit:
+ prev_primes.append(to_yield)
+ yield to_yield
+ break
+ if to_yield % prime == 0:
+ break
+ to_yield += 1
+
+
+def alphabet(*, code=None, letters=None):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+ if letters:
+ return iter(letters)
+ if code:
+ return iter(alphabets[code])
+
+
+def intertwined_sequences(iter_obj, *, generator_definitions={}):
+ using_funcs = []
+ returned_generators = []
+ for vector in iter_obj:
+ passed_args = {
+ key: val for key, val in vector.items() if key != 'length'
+ and key != 'sequence'
+ }
+ func = vector['sequence']
+ if func not in using_funcs:
+ if func in generator_definitions:
+ returned_generators.append(
+ generator_definitions[func](**passed_args)
+ )
+ else:
+ returned_generators.append(globals()[func](**passed_args))
+ else:
+ returned_generators.append(
+ returned_generators[using_funcs.index(func)]
+ )
+ using_funcs.append(func)
+ for _ in range(vector['length']):
+ yield next(returned_generators[-1])