Диан обнови решението на 01.04.2015 15:51 (преди над 9 години)
+import math
+
+
+def fibonacci():
+ f1, f2 = 1, 1
+ while True:
+ yield f1
+ f1, f2 = f2, f1 + f2
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def is_prime(n):
+ for i in range(2, int(math.sqrt(n) + 1)):
+ if n % i == 0:
+ return False
+ return n > 1
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ return (n for n in kwargs['letters'])
+ elif 'code' in kwargs:
+ if kwargs['code'] == 'lat':
+ return (chr(n) for n in range(ord('a'), ord('z') + 1))
+ elif kwargs['code'] == 'bg':
+ return (chr(n) for n in range(ord('а'), ord('я') + 1) if n != ord('ы') and n != ord('э'))
+
+
+from itertools import chain, islice
+
+
+def intertwined_sequences(*args, **kwargs):
+ generators_items = args[0]
+ generators_shortcuts = {'fibonacci': fibonacci(),
+ 'primes': primes(),
+ 'alphabet': alphabet}
+
+ result_generator = chain()
+ if 'generator_definitions' in kwargs:
+ generators_shortcuts.update(kwargs['generator_definitions'])
+
+ for current_generator_item in generators_items:
+ result_generator = chain(result_generator, islice(generators_shortcuts[current_generator_item['sequence']](),
+ current_generator_item['length']))
+
+ return result_generator
+
+