Калоян обнови решението на 31.03.2015 22:02 (преди над 9 години)
+def fibonacci():
+ x, y = 0, 1
+ while True:
+ yield y
+ x, y = y, x+y
+
+
+def primes():
+ x = 3
+ yield 2
+ while True:
+ yield x
+ indicator = 0
+ x = x+1
+ while indicator == 0:
+ a = x
+ y = x//2
+ for i in range(2, y+1):
+ if x % i == 0:
+ x = x+1
+ break
+ if a == x:
+ indicator = 1
+
+
+def alphabet(*, letters=False, code=False):
+ if letters:
+ the_yield = iter(letters)
+ while True:
+ yield next(the_yield)
+ elif code == 'bg':
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ the_yield = iter(bg)
+ while True:
+ yield next(the_yield)
+ elif code == 'lat':
+ lat = 'abcdefghijklmnopqrstuvwxyz'
+ the_yield = iter(lat)
+ while True:
+ yield next(the_yield)
+ else:
+ raise ValueError('Incorrect input!')
+
+
+#
+def ones():
+ while True:
+ yield 1
+
+
+def naturals():
+ number = 1
+ while True:
+ yield number
+ number += 1
+
+
+def multiples_of(num):
+ i = 1
+ while True:
+ yield num * i
+ i += 1
+#
+
+
+def intertwined_sequences(the_data, generator_definitions=False):
+ # k_g_f_dict -> known_generator_functions_dict
+ k_g_f_dict = {
+ 'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet
+ }
+ # i_g_d -> instanced_generators_dict
+ i_g_dict = {}
+ # c_c_dict -> corresponding_count_dict
+ c_c_dict = {}
+
+ infinite_generator_alert = False
+ terminator = 0
+
+ if generator_definitions:
+ for key, value in generator_definitions.items():
+ k_g_f_dict[key] = value
+
+ import types
+ if isinstance(the_data, types.GeneratorType):
+ infinite_generator_alert = True
+
+
+ while terminator == 0:
+
+ if infinite_generator_alert:
+ helping_list = []
+ helping_list.append(next(the_data))
+ data = helping_list
+ lenght_of_range = 1
+ else:
+ data = the_data
+ lenght_of_range = len(data)
+
+ for i in range(lenght_of_range):
+ type_of_sequence = data[i]['sequence']
+ if type_of_sequence not in i_g_dict.keys():
+ indicator = 0
+ for key in data[i].keys():
+ argument_list = []
+ if key not in ['sequence', 'length']:
+ if type_of_sequence == 'alphabet':
+ if 'code' in data[i].keys():
+ i_g_dict[type_of_sequence] = alphabet(code=data[i]['code'])
+ elif 'letters' in data[i].keys():
+ i_g_dict[type_of_sequence] = alphabet(letters=data[i]['letters'])
+ c_c_dict[type_of_sequence] = 0
+ indicator = 1
+ else:
+ # caution for argument position
+ argument_list.append(data[i][key])
+ i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence](*argument_list)
+ c_c_dict[type_of_sequence] = 0
+ indicator = 1
+
+ if indicator == 0:
+ i_g_dict[type_of_sequence] = k_g_f_dict[type_of_sequence]()
+ c_c_dict[type_of_sequence] = 0
+
+ for i in range(lenght_of_range):
+ type_of_sequence = data[i]['sequence']
+ counter = c_c_dict[type_of_sequence]
+ c_c_dict[type_of_sequence] += data[i]['length']
+
+ while counter < c_c_dict[type_of_sequence]:
+ yield next(i_g_dict[type_of_sequence])
+ counter = counter + 1
+
+ if not infinite_generator_alert:
+ terminator = 1