Георги обнови решението на 03.04.2015 16:45 (преди над 9 години)
+from itertools import islice
+
+
+def memorize(func):
+ memory = {}
+
+ def memorized(*args):
+ if args in memory:
+ return memory[args]
+ result = func(*args)
+ memory[args] = result
+ return result
+ return memorized
+
+
+@memorize
+def fib(number):
+ if number in [0, 1]:
+ return 1
+ return fib(number - 1) + fib(number - 2)
+
+
+def fibonacci():
+ value = 0
+ while True:
+ yield fib(value)
+ value += 1
+
+
+def is_prime(number):
+ for i in range(2, number - 1):
+ if number % i == 0:
+ return False
+ return True
+
+
+def primes():
+ value = 2
+ while True:
+ if is_prime(value) is True:
+ yield value
+ value += 1
+
+
+def alphabet(code='', letters=''):
+ def generate_alphabet(letters):
+ counter = 0
+ while counter < len(letters):
+ yield letters[counter]
+ counter += 1
+ raise StopIteration
+ types_of_alphabet = {
+ 'lat': list(map(chr, range(97, 123))),
+ 'bg': 'абвгдежзийклмнопстуфхцчшщъьюя'
+ }
+ if letters != '':
+ return generate_alphabet(letters)
+ for key in types_of_alphabet:
+ if code == key:
+ return generate_alphabet(types_of_alphabet[key])
+
+
+def intertwined_sequences(iterable_object, generator_definitions={}):
+ def alphabet_iterator():
+ for dictionary in iterable_object:
+ if 'code' in dictionary:
+ return alphabet(code=dictionary['code'])
+ elif 'letters' in dictionary:
+ return alphabet(letters=dictionary['letters'])
+
+ def generate_slice(sequence, length):
+ for obj in islice(sequence, 0, length):
+ yield (obj)
+ sequences = {
+ 'fibonacci': fibonacci(),
+ 'primes': primes(),
+ 'alphabet': alphabet_iterator()
+ }
+ for dictionary in iterable_object:
+ for key in dictionary:
+ if key != 'sequence' and key != 'length':
+ arg = dictionary[key]
+ for key in generator_definitions:
+ temp = generator_definitions[key]
+ try:
+ obj = temp(arg)
+ except:
+ obj = temp()
+ generator_definitions[key] = obj
+
+ sequences.update(generator_definitions)
+ for dictionary in iterable_object:
+ length = dictionary['length']
+ for key in sequences:
+ if dictionary['sequence'] == key:
+ temp = iter(generate_slice(iter(sequences[key]), length))
+ for obj in islice(temp, 0, length):
+ yield obj
+
+
+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