Калоян обнови решението на 02.04.2015 14:25 (преди над 9 години)
+import math
+from itertools import islice
+import itertools
+
+
+def fibonacci():
+ a, b = 0, 1
+ while True:
+ if a is not 0:
+ yield a
+ b = a+b
+ yield b
+ a = a+b
+
+
+def is_prime(number):
+ if number > 1:
+ if number == 2:
+ return True
+ if number % 2 == 0:
+ return False
+ for current in range(3, int(math.sqrt(number) + 1), 2):
+ if number % current == 0:
+ return False
+ return True
+ return False
+
+
+def primes():
+ i = 1
+ while True:
+ i = i+1
+ if is_prime(i):
+ yield i
+
+
+def alphabet(code, letters=None):
+ if letters is not None:
+ return (x for x in letters)
+ else:
+ if code is 'lat':
+ return (chr(x) for x in range(97, 123))
+ elif code is 'bg':
+ return (chr(x) for x in range(1072, 1104))
+
+
+def get_arg(generator_map):
+ for key, value in generator_map.items():
+ if key is not 'sequence' and key is not 'length':
+ return value
+
+
+def intertwined_sequences(generators, generator_definitions=None):
+ generator_list = {}
+ for generator in generators:
+ name = generator['sequence']
+ if name not in generator_list:
+ if name in globals():
+ if len(generator) is 2:
+ generator_list[name] = globals()[name]()
+ else:
+ generator_list[name] = globals()[name](get_arg(generator))
+ else:
+ generator_list[name] = (x for x in
+ generator_definitions[name]())
+ result = [list(islice(generator_list[x['sequence']], x['length']))
+ for x in generators]
+ return list(itertools.chain.from_iterable(result))