Елена обнови решението на 30.03.2015 20:49 (преди над 9 години)
+# -*- coding: utf-8 -*-
+
+from math import sqrt, floor
+from string import ascii_lowercase
+from itertools import islice
+
+
+BULGARIAN = u''.join(list(map(unichr, range(ord(u'а'), ord(u'я')+1))))
+
+
+def fibonacci():
+ x, y = 0, 1
+ while True:
+ yield y
+ x, y = y, x+y
+
+
+def primes():
+ x = 2
+ while True:
+ if all(map(lambda i: x % i, range(2, (1 + int(sqrt(x)))))):
+ yield x
+ x = x + 1
+
+
+def alphabet(code=None, letters=None):
+ it = iter(letters or (ascii_lowercase if code == 'lat' else BULGARIAN))
+ while True:
+ try:
+ yield next(it)
+ except StopIteration:
+ return
+
+
+def composite_generator(generators):
+ for times, generator in generators:
+ for _ in range(0, times):
+ yield next(generator)
+
+
+def intertwined_sequences(seqs, generator_definitions={}):
+ generators = {'fibonacci': fibonacci(), 'primes': primes()}
+
+ def create(name, seq):
+ if name == 'alphabet':
+ return alphabet(seq.get("code", None), seq.get("letters", None))
+ return generator_definitions[name](**seq)
+
+ def get(seq):
+ key = seq.get("sequence")
+ if not(key in generators):
+ generators[key] = create(key, seq)
+ return generators.get(key)
+
+ return composite_generator([(seq["length"], get(seq)) for seq in seqs])