Михаил обнови решението на 02.04.2015 03:05 (преди над 9 години)
+def fibonacci():
+ yield(1)
+ last, num = 1, 1
+ while True:
+ yield(num)
+ tmp = num
+ num += last
+ last = tmp
+
+
+def primes():
+ yield(2)
+ num = 2
+ while True:
+ num += 1
+ if len([x for x in range(2, num) if num % x == 0]) == 0:
+ yield(num)
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ for letter in kwargs['letters']:
+ if letter.lower() == letter:
+ yield(letter)
+ elif 'code' in kwargs:
+ letter, end = None, None
+ if kwargs['code'] == 'lat':
+ letter, end = 'a', 'z'
+ elif kwargs['code'] == 'bg':
+ letter, end = 'а', 'я'
+ else:
+ return
+ while letter != chr(ord(end) + 1):
+ if letter == 'э' or letter == 'ы':
+ letter = chr(ord(letter) + 1)
+ yield(letter)
+ letter = chr(ord(letter) + 1)
+
+
+def intertwined_sequences(format, **kwargs):
+ defs = kwargs.get('generator_definitions', {})
+ excluded = ['sequence', 'length']
+ for name, func in defs.items():
+ for x in format:
+ if x['sequence'] == name:
+ args = {k: v for k, v in x.items() if k not in excluded}
+ defs[name] = func, args
+ break
+ generators = {k: v[0](**v[1]) for k, v in defs.items()}
+ for elem in format:
+ seq = elem['sequence']
+ if seq not in generators:
+ if seq == 'alphabet':
+ generators[seq] = alphabet(**elem)
+ else:
+ generators[seq] = globals()[seq]()
+ for _ in range(elem['length']):
+ yield(next(generators[seq]))