Христиан обнови решението на 31.03.2015 22:52 (преди над 9 години)
+from math import sqrt
+
+
+def fibonacci():
+ fib0 = 0
+ fib1 = 1
+ while True:
+ yield fib1
+ temp = fib0
+ fib0 = fib1
+ fib1 = temp + fib1
+
+
+def primes():
+ num = 2
+ # yield num
+ while True:
+ if num in [2, 3, 5, 7]:
+ yield num
+ else:
+ if num not in [4, 6, 8]:
+ limit = int(sqrt(num)) + 1
+ is_prime = all([num % index != 0 for index in range(2,
+ limit)])
+ if is_prime:
+ yield num
+ num += 1
+
+
+def alphabet(code="", letters=iter([])):
+ if code == "lat":
+ return (chr(index) for index in range(97, 123))
+ elif code == "bg":
+ return (chr(index) for index in range(1072, 1104) if index not in
+ [1101, 1099])
+ else:
+ return (letters[index] for index in range(len(letters)))
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ fibonacci_sequence = []
+ primes_sequence = []
+ alphabet_sequence = []
+ extra_data = {}
+ has_parameter = False
+ for sequence in sequences:
+ if (generator_definitions != {} and
+ sequence["sequence"] in generator_definitions.keys()):
+ if len(sequence) >= 3:
+ temp = []
+ has_parameter = True
+ for item in sequence.keys():
+ if item != "sequence" and item != "length":
+ temp.append(sequence[item])
+ sequence_name = sequence["sequence"]
+ if extra_data.get(sequence_name) is None:
+ if not has_parameter:
+ extra_data[sequence_name] = \
+ iter(generator_definitions[sequence_name]())
+ else:
+ extra_data[sequence_name] = \
+ iter(generator_definitions[sequence_name](*temp))
+ for i in range(sequence["length"]):
+ yield next(extra_data[sequence_name])
+ if sequence["sequence"] == "fibonacci":
+ if fibonacci_sequence == []:
+ fibonacci_sequence = fibonacci()
+ for i in range(sequence["length"]):
+ yield next(fibonacci_sequence)
+ elif sequence["sequence"] == "primes":
+ if primes_sequence == []:
+ primes_sequence = primes()
+ for i in range(sequence["length"]):
+ yield next(primes_sequence)
+ elif sequence["sequence"] == "alphabet":
+ if alphabet_sequence == []:
+ alphabet_sequence = alphabet(sequence["code"])
+ for i in range(sequence["length"]):
+ yield next(alphabet_sequence)