Иван обнови решението на 02.04.2015 23:43 (преди над 9 години)
+def get_fibonacci_number(n):
+ if n <= 2:
+ return 1
+ else:
+ return get_fibonacci_number(n - 1) + get_fibonacci_number(n - 2)
+
+
+def fibonacci():
+ current_element = 1
+ while True:
+ yield get_fibonacci_number(current_element)
+ current_element += 1
+
+
+def is_prime(number):
+ if number < 2:
+ return False
+ elif number == 2:
+ return True
+ for divisor in range(2, int(number ** 0.5) + 1):
+ if number % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ current_prime = 2
+ while True:
+ if is_prime(current_prime):
+ yield current_prime
+ current_prime += 1
+
+
+def alphabet(*, code="", letters=""):
+ alphabet = iter(letters)
+ flag = False
+ for letter in alphabet:
+ flag = True
+ yield letter
+ if flag:
+ raise StopIteration
+ if code == "bg":
+ alphabet = iter(["а", "б", "в", "г", "д", "е", "ж", "з", "и",
+ "й", "к", "л", "м", "н", "о", "п", "р", "с",
+ "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ",
+ "ь", "ю", "я"])
+ while True:
+ yield next(alphabet)
+ elif code == "lat":
+ alphabet = map(chr, range(97, 123))
+ while True:
+ yield next(alphabet)
+
+
+def intertwined_sequences(sequences, *, generator_definitions={}):
+ defined_generators = {}
+ for key, value in generator_definitions.items():
+ generator = value()
+ defined_generators[key] = generator
+
+ fibonacci_numbers = fibonacci()
+ prime_numbers = primes()
+ for current_sequence in sequences:
+ if current_sequence["sequence"] == "fibonacci":
+ for index in range(current_sequence["length"]):
+ yield next(fibonacci_numbers)
+ elif current_sequence["sequence"] == "primes":
+ for index in range(current_sequence["length"]):
+ yield next(prime_numbers)
+ elif current_sequence["sequence"] == "alphabet":
+ alph_code = ""
+ alph_letters = ""
+ if "code" in current_sequence:
+ alph_code = current_sequence["code"]
+ else:
+ alph_letters = current_sequence["letters"]
+ generator = alphabet(code = alph_code, letters = alph_letters)
+ for index in range(current_sequence["length"]):
+ yield next(generator)
+ else:
+ generator = current_sequence["sequence"]
+ for index in range(current_sequence["length"]):
+ yield next(defined_generators[generator])