Красимир обнови решението на 31.03.2015 09:41 (преди над 9 години)
+import string
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ num = 2
+ while True:
+ for iter_num in range(2, num):
+ if num % iter_num == 0:
+ break
+ else:
+ yield num
+ num += 1
+
+
+def alphabet(code=None, letters=None):
+ if letters is not None:
+ letters = letters
+ else:
+ if code == 'bg':
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ else:
+ letters = string.ascii_lowercase
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(generator, generator_definitions=None):
+ call_dict = {}
+ for gen in generator:
+ if generator_definitions is not None:
+ for key, value in generator_definitions.items():
+ if key == gen['sequence'] and key not in call_dict:
+ globals()[gen['sequence']] = value
+ call_func, call_dict = call_func_and_dict_func(gen, call_dict)
+ for length in range(gen['length']):
+ yield next(call_func)
+
+
+def get_params(dictionary, keys):
+ key_set = set(dictionary.keys()) - set(keys)
+ return {key: dictionary[key] for key in key_set}
+
+
+def call_func_and_dict_func(gen, call_dict):
+ if gen['sequence'] in call_dict:
+ return call_dict[gen['sequence']], call_dict
+ exclude_keys = ['sequence', 'length']
+ params = get_params(gen, exclude_keys)
+ if params:
+ call_func = globals()[gen['sequence']](**params)
+ else:
+ call_func = globals()[gen['sequence']]()
+ call_dict[gen['sequence']] = call_func
+
+ return call_func, call_dict