Красимир обнови решението на 03.04.2015 16:43 (преди над 9 години)
+def fibonacci():
+ yield 1
+ prev = 0
+ after = 1
+ while True:
+ result = prev + after
+ prev = after
+ after = result
+ yield result
+
+
+def primes():
+ yield 2
+ yield 3
+ pl = [2, 3]
+ pp = 3
+ while True:
+ pp += 2
+ for i in pl:
+ if pp % i == 0:
+ break
+ else:
+ pl.append(pp)
+ yield pp
+
+
+def alphabet(*, code='bg', letters=''):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+
+ seq = ''
+ if len(letters) > 0:
+ seq = letters
+ else:
+ seq = alphabets[code]
+
+ for el in seq:
+ yield el
+
+
+default_generator_definitions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+}
+
+
+def intertwined_sequences(specs, *, generator_definitions=None):
+ gen = None
+ it = None
+ gen_iterators = {}
+ specs_copy = specs[:]
+
+ if generator_definitions is None:
+ generator_definitions = default_generator_definitions
+
+ for spec in specs_copy:
+ gen_key = spec['sequence']
+ gen_length = spec['length']
+ del spec['sequence']
+ del spec['length']
+
+ if gen_key in gen_iterators:
+ it = gen_iterators[gen_key]
+ else:
+ func = generator_definitions[gen_key]
+ #if len(spec) > 0:
+ gen = func(**spec)
+ #else:
+ #gen = func()
+ it = iter(gen)
+ gen_iterators[gen_key] = it
+
+ for _ in range(gen_length):
+ yield next(it)