Йоан обнови решението на 29.03.2015 22:24 (преди над 9 години)
+def fibonacci():
+ current, next = 1, 1
+
+ while True:
+ yield current
+ current, next = next, current + next
+
+
+def primes():
+ current = 1
+ found_primes = []
+ while True:
+ if __is_prime(current) and current not in found_primes:
+ found_primes.append(current)
+ yield current
+ else:
+ current += 1
+
+
+def alphabet(code='lat', letters=None):
+ types = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщьъюя'
+ }
+
+ input_alphabet = []
+
+ if (letters is not None):
+ input_letters = list(letters)
+ else:
+ input_letters = list(types[code])
+
+ for letter in input_letters:
+ yield letter
+
+
+def intertwined_sequences(iterators, generator_definitions={}):
+ sequences = {}
+ generic_params = ('sequence', 'length')
+
+ for iterator in iterators:
+ sequence = iterator['sequence']
+
+ params = {key: value for key, value
+ in iterator.items()
+ if key not in generic_params}
+
+ if sequence not in sequences:
+ if sequence in generator_definitions:
+ sequences[sequence] = generator_definitions[sequence](**params)
+ else:
+ sequences[sequence] = eval(sequence)(**params)
+
+ length = int(iterator['length'])
+
+ for i in range(0, length):
+ yield next(sequences[sequence])
+
+
+def __is_prime(number):
+ """http://en.wikipedia.org/wiki/Primality_test"""
+ if number <= 3:
+ return number >= 2
+ if number % 2 == 0 or number % 3 == 0:
+ return False
+ for i in range(5, int(number ** 0.5) + 1, 6):
+ if number % i == 0 or number % (i + 2) == 0:
+ return False
+ return True
- Разменил си две букви от българксата азбука. :)
-
letters
може да е безкрайна поредица. При опит да конструираш списък от такава се получаваMemoryError
.