Михаел обнови решението на 31.03.2015 01:12 (преди над 9 години)
+from itertools import count
+import string
+
+
+def intertwined_sequences(orders, generator_definitions={}):
+
+ generators = {}
+ for order in orders:
+ sequence_name = order['sequence']
+ length = order['length']
+ del order['length']
+ del order['sequence']
+
+ if sequence_name not in generators:
+ definition = globals().get(sequence_name)
+ if not definition:
+ definition = generator_definitions[sequence_name]
+ generators[sequence_name] = definition(**order)
+
+ for iterations in range(0, length):
+ yield next(generators[sequence_name])
+
+
+def fibonacci():
+ prev_value = 0
+ next_value = 1
+ while True:
+ # 1 is the first fib number according the tests provided
+ yield next_value
+ new_value = prev_value + next_value
+ prev_value = next_value
+ next_value = new_value
+
+
+def primes():
+ """ better solution here:
+ http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/
+ """
+ return (x for x in count() if is_prime(x))
+
+
+def is_prime(n):
+ """ naive primality test """
+
+ if n <= 3:
+ return n >= 2
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def alphabet(**kwargs):
+ letters = kwargs.setdefault('letters', [])
+ if not letters:
+ return (c for c in get_alphabet(kwargs['code']))
+ else:
+ return (c.lower() for c in list(letters))
+
+
+def get_alphabet(code):
+ codes = {
+ 'bg': [
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
+ 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
+ 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+ ],
+ 'lat': list(string.ascii_lowercase)
+ }
+
+ return codes.setdefault(code, [])