Николай обнови решението на 28.03.2015 20:10 (преди над 9 години)
+import math
+
+
+def fibonacci():
+ current_number = 1
+ next_number = 1
+
+ yield current_number
+ yield next_number
+
+ while True:
+ next_number = current_number + next_number
+ yield next_number
+ current_number = next_number - current_number
+
+
+def is_prime(number):
+ flag = True
+ current_divider = 2
+ while flag and current_divider <= math.sqrt(number):
+ if number % current_divider == 0:
+ flag = False
+ current_divider = current_divider + 1
+ return flag
+
+
+def primes():
+ current_number = 2
+ yield current_number
+
+ current_number = current_number + 1
+ yield current_number
+
+ splitter = 2
+ while True:
+ current_number = current_number + splitter
+ if is_prime(current_number):
+ yield current_number
+
+
+def alphabet(letters=None, code='lat'):
+ if letters is not None:
+ for letter in letters:
+ yield letter
+
+ elif code == 'lat':
+ symbol = 'a'
+ counter = 0
+ while counter < 26:
+ yield symbol
+ symbol = chr(ord(symbol) + 1)
+ counter = counter + 1
+
+ elif code == 'bg':
+ symbol = 'а'
+ counter = 0
+ while counter < 30:
+ yield symbol
+
+ symbol = chr(ord(symbol) + 1)
+ if symbol == 'э' or symbol == 'ы':
+ symbol = chr(ord(symbol) + 1)
+ counter = counter + 1
+
+
+def is_started(generators, sequence):
+ for generator_name in generators:
+ if generator_name == sequence:
+ return True
+
+ return False
+
+
+def get_parameters(items):
+ for key in items:
+ if key is not 'sequence' and key is not 'length' and key is not 'code':
+ return key
+ return None
+
+
+def intertwined_sequences(generator, generator_definitions=None):
+ generators = {}
+
+ for current_item in generator:
+ current_sequence = current_item['sequence']
+ current_length = current_item['length']
+
+ if current_sequence == 'fibonacci':
+ if not is_started(generators, current_sequence):
+ generators[current_sequence] = fibonacci()
+
+ fibonacci_numbers = generators[current_sequence]
+ while current_length > 0:
+ yield next(fibonacci_numbers)
+ current_length = current_length - 1
+
+ elif current_sequence == 'primes':
+ if not is_started(generators, current_sequence):
+ generators[current_sequence] = primes()
+
+ prime_numbers = generators[current_sequence]
+ while current_length > 0:
+ yield next(prime_numbers)
+ current_length = current_length - 1
+
+ elif current_sequence == 'alphabet':
+ current_code = current_item['code']
+ current_alphabet = current_sequence + str(current_code)
+
+ if not is_started(generators, current_alphabet):
+ generators[current_alphabet] = alphabet(code=current_code)
+
+ alphabet_symbols = generators[current_alphabet]
+ while current_length > 0:
+ yield next(alphabet_symbols)
+ current_length = current_length - 1
+ else:
+ if not is_started(generators, current_sequence):
+ current_generator = generator_definitions[current_sequence]
+ parameters = get_parameters(current_item)
+
+ if parameters is not None:
+ argument = current_item[parameters]
+ generators[current_sequence] = current_generator(argument)
+ else:
+ generators[current_sequence] = current_generator()
+
+ custom_generator = generators[current_sequence]
+ while current_length > 0:
+ yield next(custom_generator)
+ current_length = current_length - 1
- Разичташ на това, че елементите за alphabet в списъка подаден на
intertwined_sequences
винаги ще имат ключ'code'
, което не отговаря на условието. - Предаването на параметри към конструкторите от
generator_definitions
не се получава.