Галина обнови решението на 02.04.2015 23:18 (преди над 9 години)
+from itertools import islice
+
+
+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def is_prime(n):
+ for i in range(2, int(n ** 0.5) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(**kwargs):
+ for key, value in kwargs.items():
+ if key == "letters":
+ for char in islice(value, 0, None):
+ yield char
+ break
+ elif value == "bg" and "letters" not in kwargs.keys():
+ letters = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ for char in islice(letters, 0, None):
+ yield char
+ elif value == "lat" and "letters" not in kwargs.keys():
+ letters = "abcdefghijklmnopqrstuvwxyz"
+ for char in islice(letters, 0, None):
+ yield char
+
+
+def intertwined_sequences(seq, **kwargs):
+ fib_numbers = fibonacci()
+ prime_numbers = primes()
+ for item in seq:
+ length = item["length"]
+ if item["sequence"] == "fibonacci":
+ while length > 0:
+ yield next(fib_numbers)
+ length -= 1
+ elif item["sequence"] == "primes":
+ while length > 0:
+ yield next(prime_numbers)
+ length -= 1
+ elif item["sequence"] == "alphabet":
+ if "letters" in item.keys():
+ letters_char = alphabet(letters=item["letters"])
+ while length > 0:
+ yield next(letters_char)
+ length -= 1
+ else:
+ characters = alphabet(code=item["code"])
+ while length > 0:
+ yield next(characters)
+ length -= 1
seq
може да е безкрайна редица. В такъв случай този for
никога няма да приключи ако бъде подаден generator_definitions
. Ако сложиш един else
в края това би помогнало.