София обнови решението на 03.04.2015 16:19 (преди над 9 години)
+def fibonacci():
+ first = 1
+ second = 1
+ yield first
+ while True:
+ yield second
+ first, second = second, first + second
+
+
+def primes():
+ first = 2
+ while True:
+ if is_prime(first):
+ yield first
+ first += 1
+
+
+def is_prime(number):
+ for n in range(2, number):
+ if number % n == 0:
+ return False
+ return True
+
+
+def alphabet(code='bg', letters=None):
+ if letters is None:
+ alphabets = {
+ 'lat': [chr(element) for element in range(ord('a'), ord('z') + 1)],
+ 'bg': [chr(element) for element in range(ord('а'), ord('я') + 1)
+ if chr(element) not in 'ыэ']
+ }
+ for letter in alphabets[code]:
+ yield letter
+ else:
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(iterable, generator_definitions=None):
+ generator_functions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+ if generator_definitions is not None:
+ generator_functions.update(generator_definitions)
+ generators = {}
+ for item in iterable:
+ if not item['sequence'] in generators:
+ keyword_args = {key: value
+ for key, value in item.items()
+ if key not in ['sequence', 'length']}
+ generator = generator_functions[item['sequence']](**keyword_args)
+ generators[item['sequence']] = generator
+ for _ in range(item['length']):
+ yield next(generators[item['sequence']])