Александър обнови решението на 01.04.2015 13:52 (преди над 9 години)
+LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
+BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+def fibonacci():
+ a = b = 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ for divider in range(2, int(number / 2) + 1):
+ if not number % divider:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(code='xx', letters=''):
+ if not len(letters):
+ if code == 'lat':
+ letters = LATIN_ALPHABET
+ else:
+ letters = BULGARIAN_ALPHABET
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(input_generators, generator_definitions={}):
+ generators = {}
+ for generator in input_generators:
+ sequence = generator['sequence']
+ length = generator['length']
+ if sequence not in generators:
+ if sequence == 'fibonacci':
+ generators[sequence] = fibonacci()
+ for _ in range(length):
+ yield next(generators[sequence])
+ elif sequence == 'primes':
+ generators[sequence] = primes()
+ for _ in range(length):
+ yield next(generators[sequence])
+
+ elif sequence == 'alphabet':
+ if 'letters' in generator:
+ generators[sequence] = alphabet(
+ letters=generator['letters'])
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ generators[sequence] = alphabet(code=generator['code'])
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ generator.pop('sequence')
+ generator.pop('length')
+ kwargs = generator
+ gen_func = iter(generator_definitions[sequence](**kwargs))
+ generators[sequence] = gen_func
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ for _ in range(length):
+ yield next(generators[sequence])