Цветелина обнови решението на 01.04.2015 16:09 (преди над 9 години)
+def fibonacci():
+ first_num, second_num = 1, 1
+ while 1:
+ yield first_num
+ first_num, second_num = second_num, first_num + second_num
+
+
+def primes():
+ current = 2
+ while 1:
+ yield current
+ current += 1
+ while not is_prime(current):
+ current += 1
+
+
+def is_prime(number):
+ for i in range(2, number):
+ if number % i == 0:
+ return False
+ return True
+
+
+def alphabet(**kwargs):
+ predefined_letters = {
+ 'bg': 'абвгдежзийклмнопрстуфхюцшщъьюя',
+ 'lat': 'abcdefghijklmnopqrstuvwxyz'
+ }
+ if kwargs is not None:
+ for key, value in kwargs.items():
+ if key == 'letters':
+ return gen_alphabet(value)
+ if key == 'code':
+ return gen_alphabet(predefined_letters[value])
+
+
+def gen_alphabet(letters):
+ current = 0
+ while 1:
+ try:
+ yield letters[current]
+ current += 1
+ except IndexError:
+ raise StopIteration
+
+
+iter_definitions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet
+}
+
+
+def intertwined_sequences(iterable_object, **kwargs):
+ used_iterables = []
+ if kwargs is not None:
+ if 'generator_definitions' in kwargs:
+ iter_definitions.update(kwargs['generator_definitions'])
+ for iterable in iterable_object:
+ if not any(item['sequence'] == iterable['sequence']
+ for item in used_iterables):
+ iterable_params = {key: value for key, value in iterable.items()
+ if not key == 'sequence' and
+ not key == 'length'}
+ if iterable_params:
+ current = iter(iter_definitions[
+ iterable['sequence']](**iterable_params))
+ else:
+ current = iter(iter_definitions[iterable['sequence']]())
+ used_iterables.append({
+ 'sequence': iterable['sequence'],
+ 'iter': current
+ })
+ for used_iterable in used_iterables:
+ if iterable['sequence'] == used_iterable['sequence']:
+ if 'length' in iterable:
+ for i in range(0, iterable['length']):
+ yield next(used_iterable['iter'])
+ else:
+ yield next(used_iterable['iter'])
Имаш излишна буква в българската азбука :)