Стоян обнови решението на 02.04.2015 01:48 (преди над 9 години)
+def fibonacci():
+ first = 1
+ second = 1
+ yield first
+ yield second
+ while True:
+ last = first
+ first = second
+ second = first + last
+ yield second
+
+
+def is_prime(number):
+ return len([x for x in range(1, number) if number % x == 0]) == 1
+
+
+def get_next_prime(number):
+ while True:
+ number += 1
+ if is_prime(number):
+ return number
+
+
+def primes():
+ a = 2
+ yield a
+ while True:
+ a = get_next_prime(a)
+ yield a
+
+
+def alphabet(code="", letters=""):
+ if letters != "":
+ i = 0
+ while i <= len(letters):
+ yield letters[i]
+ i += 1
+ else:
+ if code == "bg":
+ i = ord("а")
+ while i <= ord("я"):
+ yield chr(i)
+ i += 1
+ elif code == "lat":
+ i = ord("a")
+ while i <= ord("z"):
+ yield chr(i)
+ i += 1
+ else:
+ raise StopIteration
+
+
+def intertwined_sequences(sequences, **kwargs):
+ fibonacci_generator = fibonacci()
+ primes_generator = primes()
+ alphabet_generator = alphabet()
+ for dictionary in sequences:
+ i = dictionary["length"]
+ if "letters" in dictionary:
+ alphabet_generator = alphabet(letters=dictionary["letters"])
+ elif "code" in dictionary:
+ alphabet_generator = alphabet(code=dictionary["code"])
+ sequence = dictionary["sequence"]
+ while i > 0:
+ if sequence == "fibonacci":
+ yield next(fibonacci_generator)
+ elif sequence == "primes":
+ yield next(primes_generator)
+ elif sequence == "alphabet":
+ yield next(alphabet_generator)
+ elif "generator_definitions" in kwargs and \
+ sequence in kwargs["generator_definitions"]:
+ generator_definitions = kwargs["generator_definitions"]
+
+ if len(dictionary) > 2:
+ for k, v in iter(dictionary.items()):
+ if k != 'sequence' and k != 'length':
+ yield next(generator_definitions[sequence](v))
+ else:
+ yield next(generator_definitions[sequence]())
+ else:
+ yield next(dictionary["sequence"])
+ i = i - 1