Елица обнови решението на 03.04.2015 16:54 (преди над 9 години)
+import itertools
+
+
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(n):
+ if n == 2 or n == 3:
+ return True
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def primes():
+ a = 2
+ while True:
+ if is_prime(a):
+ yield a
+ a += 1
+
+
+def alphabet(code='', letters=''):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+ letters = letters if len(letters) else alphabets[code]
+ for character in letters:
+ yield str(character)
+
+
+def intertwined_sequences(sequences, generator_definitions={
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }):
+ sequences = list(sequences)
+ for mapper in generator_definitions:
+ for seq in sequences:
+ if (seq['sequence'] != mapper):
+ continue
+ args = [item[1] for item in seq.items() if
+ item[0] not in ['length', 'sequence']]
+ if (args):
+ generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']](*args)
+ else:
+ generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']]()
+ try:
+ result = itertools.chain(list(itertools.chain((list(
+ itertools.islice(generator_definitions[seq['sequence']], 0, seq['length']))
+ for seq in sequences))))
+ return list(itertools.chain(*result))
+ except StopIteration:
+ pass