Кристиан обнови решението на 03.04.2015 12:08 (преди над 9 години)
+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ second += first
+ first = second - first
+
+
+def primes():
+ number = 2
+ primes = []
+ while True:
+ if all((number % prime for prime in primes)):
+ primes.append(number)
+ yield number
+ number += 1
+
+lat_alphabet = (chr(ord('a') + i) for i in range(26))
+bg_alphabet = (chr(ord('а') + i) for i in range(32) if i != 27 and i != 29)
+
+
+def alphabet(code=None, letters=None):
+ if letters:
+ used_letters = letters
+ elif code == 'lat':
+ used_letters = lat_alphabet
+ else:
+ used_letters = bg_alphabet
+
+ return (letter for letter in used_letters)
+
+
+def get_or_add_generator(generators, sequence, definitions):
+ generator_key = sequence['sequence']
+ generator = generators.get(generator_key, None)
+ if not generator:
+ generator_args = sequence.copy()
+ del generator_args['sequence']
+ del generator_args['length']
+
+ generator = definitions[generator_key](**generator_args)
+ generators[generator_key] = generator
+ return generator
+
+
+base_definitions = {'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet}
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ definitions = base_definitions.copy()
+ definitions.update(generator_definitions)
+ generators = {}
+ for sequence in sequences:
+ generator = get_or_add_generator(generators,
+ sequence,
+ definitions)
+ for i in range(sequence['length']):
+ yield next(generator)
+
+ raise StopIteration()