Валентин обнови решението на 02.04.2015 21:59 (преди над 9 години)
+def fibonacci():
+ num1, num2 = 1, 0
+ while True:
+ yield num1
+ num1, num2 = num1 + num2, num1
+
+
+def primes():
+ to_yield = 2
+ prev_primes = [2]
+ while True:
+ limit = to_yield ** 0.5
+ for prime in prev_primes:
+ if prime > limit:
+ prev_primes.append(to_yield)
+ yield to_yield
+ break
+ if to_yield % prime == 0:
+ break
+ to_yield += 1
+
+
+def alphabet(*, code=None, letters=None):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+ if letters:
+ return iter(letters)
+ if code:
+ return iter(alphabets[code])
+
+
+def intertwined_sequences(iter_obj, *, generator_definitions={}):
+ using_funcs = []
+ returned_generators = []
+ for vector in iter_obj:
+ passed_args = {
+ key: val for key, val in vector.items() if key != 'length'
+ and key != 'sequence'
+ }
+ func = vector['sequence']
+ if func not in using_funcs:
+ if func in generator_definitions:
+ returned_generators.append(
+ generator_definitions[func](**passed_args)
+ )
+ else:
+ returned_generators.append(globals()[func](**passed_args))
+ else:
+ returned_generators.append(
+ returned_generators[using_funcs.index(func)]
+ )
+ using_funcs.append(func)
+ for _ in range(vector['length']):
+ yield next(returned_generators[-1])