Клара обнови решението на 03.04.2015 10:17 (преди над 9 години)
+import string
+from itertools import islice
+import itertools
+from itertools import cycle
+
+
+def primes():
+ first_prime_number = 2
+ primes = set()
+ while True:
+ for number in primes:
+ if first_prime_number % number == 0:
+ break
+ else:
+ primes.add(first_prime_number)
+ yield first_prime_number
+ first_prime_number += 1
+
+
+def fibonacci():
+ first_number, second_number, counter = 1, 1, 0
+ while True:
+ yield first_number
+ first_number, second_number = second_number, first_number + \
+ second_number
+ counter += 1
+
+
+def alphabet(*, code='', letters=''):
+ lat = list(string.ascii_lowercase)
+ bg = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
+ 'з', 'и', 'й', 'к', 'л', 'м', 'н',
+ 'о', 'п', 'р', 'с', 'т', 'у', 'ф',
+ 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
+ 'ю', 'я']
+
+ if(code != '' and letters != ''):
+ index = 0
+ while True:
+ yield letters[index]
+ index = index + 1
+
+ if(code != ''):
+ if(code == 'lat'):
+ while True:
+ for letter in lat:
+ yield letter
+ if(code == 'bg'):
+ while True:
+ for l in bg:
+ yield l
+ if(letters != ''):
+ index = 0
+ while True:
+ yield letters[index]
+ index = index + 1
+
+
+def intertwined_sequences(given_dict, *, generator_definitions=''):
+ return_list = []
+
+ fibonacci_numbers = fibonacci()
+ prime_numbers = primes()
+ alphabet_bg = alphabet(code='bg')
+ alphabet_lat = alphabet(code='lat')
+ generator_def = generator_definitions
+
+ for keys in given_dict:
+ len = keys.get('length')
+ sequence = keys.get('sequence')
+ code = keys.get('code')
+ letter = keys.get('letters')
+
+ if(sequence == 'fibonacci'):
+ return_list.append(list(islice(fibonacci_numbers, len)))
+
+ if(sequence == 'primes'):
+ return_list.append(list(islice(prime_numbers, len)))
+
+ if(sequence == 'alphabet'):
+ if(code == 'bg'):
+ return_list.append(list(islice(alphabet_bg, len)))
+
+ if(code == 'lat'):
+ return_list.append(list(islice(alphabet_lat, len)))
+
+ if(code is None):
+ alphabet_ = alphabet(letters=letter)
+ return_list.append(list(islice(alphabet_, len)))
+
+ if(sequence != 'fibonacci' and sequence != 'alphabet'
+ and sequence != 'primes'):
+ for key, value in generator_def.items():
+ if(sequence == key):
+ generator_function = value()
+ return_list.append(list(islice(generator_function, len)))
+
+ for item in list(itertools.chain(*return_list)):
+ yield item
given_dict
може да бъде безкраен. В такъв случай конструирането на return_list
е много лоша идея. Цикъла никога няма да приключи и в крайна сметка генератора няма да направи нищо.