Мартин обнови решението на 03.04.2015 16:27 (преди над 9 години)
+def fibonacci():
+ current = 1
+ first = 0
+ second = 1
+ while True:
+ yield current
+ current = first + second
+ first = second
+ second = current
+
+
+def primes():
+ def is_prime(tested_number):
+ divisor = 3
+ while True:
+ quotient = tested_number // divisor
+ if quotient < divisor:
+ return True
+ if tested_number == quotient * divisor:
+ return False
+ divisor += 2
+
+ def get_next_prime_number(number):
+ if number == 2:
+ return 3
+ number += 2
+ while not is_prime(number):
+ number += 2
+ return number
+
+ current = 2
+ while True:
+ yield current
+ current = get_next_prime_number(current)
+
+
+def alphabet(**kwargs):
+ for key, value in kwargs.items():
+ if key == "letters":
+ for letter in value:
+ yield letter
+ break
+ if key == "code":
+ if value == "lat":
+ current_letter = ord("a")
+ last_letter = ord("z")
+ if value == "bg":
+ current_letter = ord("а")
+ last_letter = ord("я")
+ while current_letter <= last_letter:
+ yield chr(current_letter)
+ if current_letter == ord("ъ") or current_letter == ord("ь"):
+ current_letter += 1
+ current_letter += 1
+
+
+def intertwined_sequences(sequences_container, generator_definitions={}):
+ unique_generators = dict()
+ for sequence in sequences_container:
+ if sequence["sequence"] not in generator_definitions:
+ generator_definitions[sequence.get("sequence")] = globals()[
+ sequence.get("sequence")]
+ generator_arguments = dict()
+ for key, value in sequence.items():
+ if key != "sequence" and key != "length":
+ generator_arguments[key] = value
+ generator = generator_definitions.get(sequence["sequence"])
+ length = sequence.get("length")
+ if sequence.get("sequence") not in unique_generators:
+ if generator_arguments:
+ unique_generators[sequence.get("sequence")] = \
+ generator(**generator_arguments)
+ else:
+ unique_generators[sequence.get("sequence")] = generator()
+ for _ in range(length):
+ yield next(unique_generators[sequence.get("sequence")])