Решение на Генератори и итератори от Добромир Иванов

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

Към профила на Добромир Иванов

Резултати

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

Код

def fibonacci():
previous, following = 1, 1
while True:
yield previous
previous, following = following, previous + following
def primes():
prime = 2
while True:
for i in range(2, prime):
if prime % i == 0:
break
else:
yield prime
prime += 1
def alphabet(**kwargs):
if 'letters' in kwargs.keys():
for letter in kwargs['letters']:
yield letter
else:
start, end = ord('a'), ord('z')
if kwargs['code'] == 'bg':
start, end = ord('а'), ord('я')
while start <= end:
yield chr(start)
start += 1
def get_args(sequence):
args = {}
keys = list(set(list(sequence)) - set(['sequence', 'length']))
for key in keys:
args[key] = sequence[key]
return args
def intertwined_sequences(sequences, generator_definitions={}):
generators = {}
for sequence in sequences:
name = sequence['sequence']
args = get_args(sequence)
if name in globals().keys() and name not in generators.keys():
generator = globals()[name]
generators[name] = generator(**args)
elif name not in generators.keys():
generator = generator_definitions[name]
generators[name] = iter(generator(**get_args(sequence)))
i = 0
while i < sequence['length']:
yield next(generators[name])
i += 1

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

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

----------------------------------------------------------------------
Ran 14 tests in 3.588s

FAILED (errors=1)

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

Добромир обнови решението на 03.04.2015 11:33 (преди над 9 години)

+def fibonacci():
+ previous, following = 1, 1
+ while True:
+ yield previous
+ previous, following = following, previous + following
+
+
+def primes():
+ prime = 2
+ while True:
+ for i in range(2, prime):
+ if prime % i == 0:
+ break
+ else:
+ yield prime
+ prime += 1
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs.keys():
+ for letter in kwargs['letters']:
+ yield letter
+ else:
+ start, end = ord('a'), ord('z')
+ if kwargs['code'] == 'bg':
+ start, end = ord('а'), ord('я')
+ while start <= end:
+ yield chr(start)
+ start += 1
+
+
+def get_args(sequence):
+ args = {}
+ keys = list(set(list(sequence)) - set(['sequence', 'length']))
+ for key in keys:
+ args[key] = sequence[key]
+ return args
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ generators = {}
+ for sequence in sequences:
+ name = sequence['sequence']
+ args = get_args(sequence)
+ if name in globals().keys() and name not in generators.keys():
+ generator = globals()[name]
+ generators[name] = generator(**args)
+ elif name not in generators.keys():
+ generator = generator_definitions[name]
+ generators[name] = iter(generator(**get_args(sequence)))
+ i = 0
+ while i < sequence['length']:
+ yield next(generators[name])
+ i += 1