Гюлджан обнови решението на 01.04.2015 00:59 (преди над 9 години)
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ number = 2
+ while True:
+ is_prime = True
+ for x in range(2, number):
+ if number % x == 0:
+ is_prime = False
+ break
+ if is_prime:
+ yield number
+ number += 1
+
+
+def alphabet(*, code='lat', letters=[]):
+ if letters:
+ iterable = letters
+ else:
+ if code == 'lat':
+ iterable = list('abcdefghijklmnopqrstuvwxyz')
+ else:
+ iterable = list('абвгдежзийклмнопрстуфхцчщъьююя')
+ for letter in iterable:
+ yield letter
+
+
+def to_generator(string, code_='', letters_=[]):
+ if string == 'fibonacci':
+ return fibonacci()
+ elif string == 'primes':
+ return primes()
+ elif string == 'alphabet':
+ if letters_:
+ return alphabet(code=code_, letters=letters_)
+ else:
+ return alphabet(code=code_)
+
+
+def intertwined_sequences(items, *, generator_definitions={}):
+ generators = {}
+ for item in items:
+ sequence = item['sequence']
+ if item['sequence'] not in generators:
+ if item['sequence'] == 'alphabet':
+ if 'code' not in item:
+ generators[sequence] = to_generator('alphabet',
+ item['letters'])
+ elif 'letters' not in item:
+ generators[sequence] = to_generator('alphabet',
+ item['code'])
+ else:
+ generators[sequence] = to_generator('alphabet',
+ item['code'],
+ item['letters'])
+ elif sequence == 'fibonacci' or sequence == 'primes':
+ generators[sequence] = to_generator(sequence)
+ else:
+ generators[sequence] = iter(generator_definitions[sequence]())
+ length = item['length']
+ while length > 0:
+ yield next(generators[sequence])
+ length -= 1