Александър обнови решението на 03.04.2015 13:10 (преди над 9 години)
+from math import sqrt
+
+
+def fibonacci():
+ fib_1, fib_2 = 0, 1
+ while True:
+ fib_1, fib_2 = fib_2, fib_1 + fib_2
+ yield fib_1
+
+
+def is_prime(num):
+ if num == 2 or num == 3:
+ return True
+
+ for divisor in range(2, int(sqrt(num))+1):
+ if num % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 1
+ while True:
+ number += 1
+ if(is_prime(number)):
+ yield number
+
+
+LETTERS = {
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': 'abcdefghigklmnopqrstuvwxyz'
+ }
+
+
+def alphabet(code=None, letters=None):
+ if code in LETTERS and not letters:
+ letters = LETTERS[code]
+ for letter_index in range(len(letters)):
+ yield letters[letter_index]
+
+
+GENERATORS = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+
+
+def create(generator, generator_definitions):
+ name = generator['sequence']
+ kwargs = {}
+ for key in generator:
+ if key not in ('sequence', 'length'):
+ kwargs[key] = generator[key]
+ if name in GENERATORS:
+ new_generator = GENERATORS[name](**kwargs)
+ else:
+ new_generator = generator_definitions[name](**kwargs)
+
+ return new_generator
+
+
+def intertwined_sequences(sequence, generator_definitions=None):
+ generators = {}
+ for generator in sequence:
+ name = generator['sequence']
+ if name not in generators:
+ generators[name] = create(generator, generator_definitions)
+
+ for _ in range(generator['length']):
+ yield next(generators[name])