Александър обнови решението на 03.04.2015 16:22 (преди над 9 години)
+import types
+
+
+def fibonacci(first_number=0, second_number=1):
+ if (first_number == 0):
+ yield 1
+ yield first_number + second_number
+ yield from fibonacci(second_number, first_number + second_number)
+
+
+def is_prime(number, current_divider=2):
+ if (number % current_divider == 0) and (current_divider != number):
+ return False
+ elif number == current_divider:
+ return True
+ return is_prime(number, current_divider + 1)
+
+
+def primes(current_number=2):
+ if is_prime(current_number):
+ yield current_number
+ yield from primes(current_number + 1)
+
+
+LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
+CYRILLIC_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+class Alphabet:
+ def __init__(self, alphabet):
+ self._alphabet = alphabet
+
+ def __iter__(self):
+ return iter(self._alphabet)
+
+
+def alphabet(code='', letters=''):
+ current_alphabet = ''
+ if letters != '':
+ current_alphabet = Alphabet(letters)
+ elif code == 'bg':
+ current_alphabet = Alphabet(CYRILLIC_ALPHABET)
+ elif code == 'lat':
+ current_alphabet = Alphabet(LATIN_ALPHABET)
+ for letter in current_alphabet:
+ yield letter
+
+
+class Sequence:
+ def __init__(self, sequence='', length=0, parameter=''):
+ self.sequence = sequence
+ self.length = length
+ self.parameter = parameter
+
+
+def intertwined_sequences(sequence_descriptor, generator_definitions={}):
+ initialized_generators = {}
+ current_sequences = []
+ sequence_descriptor_internal = []
+ if isinstance(sequence_descriptor, types.GeneratorType):
+ sequence_descriptor_internal = [next(sequence_descriptor)]
+ else:
+ sequence_descriptor_internal = sequence_descriptor
+ for sequence in sequence_descriptor_internal:
+ current_sequence = Sequence()
+ for key, value in sequence.items():
+ if key == 'sequence':
+ current_sequence.sequence = value
+ if value not in initialized_generators.keys():
+ if value == 'fibonacci':
+ initialized_generators[value] = fibonacci()
+ elif value == 'primes':
+ initialized_generators[value] = primes()
+ elif value == 'alphabet':
+ initialized_generators[value] = ''
+ else:
+ if value in generator_definitions.keys():
+ initialized_generators[value] = \
+ generator_definitions[value]
+ elif key == 'length':
+ current_sequence.length = value
+ elif key == 'code':
+ current_sequence.parameter = value
+ initialized_generators['alphabet'] = alphabet(code=value)
+ elif key == 'letters':
+ current_sequence.parameter = value
+ initialized_generators['alphabet'] = \
+ alphabet(code='', letters=value)
+ else:
+ current_sequence.parameter = value
+ initialized_generators[current_sequence.sequence] = \
+ generator_definitions[current_sequence.sequence](value)
+ current_sequences.append(current_sequence)
+ for sequence in current_sequences:
+ current_generator = initialized_generators[sequence.sequence]
+ for index in range(sequence.length):
+ yield next(current_generator)
+ if isinstance(sequence_descriptor, types.GeneratorType):
+ yield from intertwined_sequences(sequence_descriptor)