Николай обнови решението на 28.03.2015 22:13 (преди над 9 години)
+from collections import defaultdict
+from itertools import count
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ def is_prime(n):
+ return not any(n % i == 0 for i in range(2, n))
+ return (prime for prime in count(2) if is_prime(prime))
+
+
+def alphabet(code='', letters=''):
+ alphabets = defaultdict(lambda: letters,
+ {'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'})
+ alphabet = letters if letters else alphabets[code]
+
+ for letter in alphabet:
+ yield letter
+
+
+def intertwined_sequences(iterable, generator_definitions={}):
+ generator_definitions.update({'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet})
+ generator_objects = {}
+ for item in iterable:
+ sequence = item['sequence']
+ length = item['length']
+ params = {}
+ for key in item.keys():
+ if key not in ['sequence', 'length']:
+ params[key] = item[key]
+
+ if sequence not in generator_objects.keys():
+ generator_objects[sequence] = \
+ generator_definitions[sequence](**params)
+
+ for i in range(length):
+ yield next(generator_objects[sequence])
В intertwined_sequences
стойностите на generator_definitions
със сигурност са итеруеми, но е възможно да не са итератори.
hint: range
обектите са такива.