Илиян обнови решението на 30.03.2015 13:45 (преди над 9 години)
+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ temp = first
+ first = second
+ second += temp
+
+
+def is_prime(n):
+ n = abs(n)
+ i = 2
+ if n == 1:
+ return False
+ while i <= (n + 1) // 2:
+ if n % i == 0:
+ return False
+ i += 1
+ return True
+
+
+def next_prime(n):
+ if is_prime(n):
+ return n
+ else:
+ return next_prime(n + 1)
+
+
+def primes():
+ first_prime = 2
+ while True:
+ current = next_prime(first_prime)
+ first_prime = current + 1
+ yield current
+
+
+def alphabet(code = "", letters = ""):
+ bulgarian = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ latin = "abcdefghijklmnopqrstuvwxyz"
+ while True:
+ if letters:
+ for x in letters:
+ yield x
+
+ elif code == 'bg':
+ for x in bulgarian:
+ yield x
+
+ else:
+ for x in latin:
+ yield x
+
+
+def intertwined_sequences(catalogue, generator_definitions={}):
+ primes_numbers = primes()
+ fibonacci_numbers = fibonacci()
+ lettering_bg = alphabet('bg')
+ lettering_lat = alphabet('lat')
+ whose_alphabet = []
+ generator_with_arg = {}
+
+ for condition in catalogue:
+ if condition['sequence'] == 'alphabet' and not whose_alphabet:
+ if 'code' in condition:
+ if condition['code'] == 'bg':
+ whose_alphabet.append(lettering_bg)
+ else:
+ whose_alphabet.append(lettering_lat)
+ else:
+ whose_alphabet.append(alphabet("", condition['letters']))
+
+ if condition['sequence'] == 'alphabet':
+ if 'code' in condition:
+ if 'code' == 'bg':
+ for x in range(1, condition['length'] + 1):
+ yield next(whose_alphabet[0])
+ else:
+ for x in range(1, condition['length'] + 1):
+ yield next(whose_alphabet[0])
+ else:
+ for x in range(condition['length']):
+ yield next(whose_alphabet[0])
+
+ elif condition['sequence'] == 'fibonacci':
+ for x in range(1, condition['length'] + 1):
+ yield next(fibonacci_numbers)
+
+ elif condition['sequence'] == 'primes':
+ for x in range(1, condition['length'] + 1):
+ yield next(primes_numbers)
+
+ else:
+ if 'num' in condition:
+ generator_with_arg[condition['sequence']] = condition['num']
+
+ if condition['sequence'] in generator_with_arg:
+ arg = generator_with_arg[condition['sequence']]
+ generator = generator_definitions[condition['sequence']](arg)
+ for x in range(condition['length']):
+ yield next(generator)
+ else:
+ generator = generator_definitions[condition['sequence']]()
+ for x in range(condition['length']):
+ yield next(generator)
За alphabet: Азбуката безкраен брой пъти ли можем да я обхождаме или след първото обхождане raise-ваме StopIteration ?