Янислав обнови решението на 01.04.2015 05:13 (преди над 9 години)
+import string
+
+BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+LATIN_ALPHABET = string.ascii_lowercase
+
+
+def fibonacci(a=1, b=1):
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ return all(number % divisor for divisor in range(2, number))
+
+
+def primes(number=2):
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(**kwargs):
+ codes = {'bg': BULGARIAN_ALPHABET, 'lat': LATIN_ALPHABET}
+ if 'letters' in kwargs:
+ for char in kwargs['letters']:
+ yield char
+ else:
+ for char in codes[kwargs['code']]:
+ yield char
+
+
+def intertwined_sequences(data, generator_definitions={}):
+ generator_definitions.update({'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet, })
+
+ # @instances holds all my generator instances with validated parameters
+ instances = {}
+ for dicty in list(data):
+ arguments = {key: value for key, value in dicty.items()
+ if not any([key == 'sequence', key == 'length'])}
+
+ # I have to initialize only one instance per sequence and if
+ # I meet second time sequence that I already initilized(arguments for them are given only first time)
+ # I have to catch TypeError
+ try:
+ instances.update(
+ {dicty['sequence']: generator_definitions[dicty['sequence']](**arguments)})
+ except TypeError:
+ continue
+
+ for dictionary in data:
+ for _ in range(dictionary['length']):
+ yield next(instances[dictionary['sequence']])
data
може да бъде безкрайна редица, тогава първия for
в intertwined_sequences
няма да приключи никога и никога няма да се стигне до yield-ването.