Тачо обнови решението на 02.04.2015 23:17 (преди над 9 години)
+def fibonacci():
+ prev = 1
+ last = 1
+ while True:
+ yield prev
+ prev, last = last, prev + last
+
+
+def primes():
+ number = 1
+
+ def is_prime(number):
+ divisors = []
+ for current in range(1, number + 1):
+ if number % current == 0:
+ divisors.append(current)
+ return divisors == [1, number]
+
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(*, code='', letters=''):
+ bg = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ lat = "abcdefghijklmnopqrstuvwxyz"
+ if letters:
+ myiterator = iter(letters)
+ while True:
+ yield next(myiterator)
+ elif code:
+ if code == 'bg':
+ myiterator = iter(bg)
+ else:
+ myiterator = iter(lat)
+ while True:
+ yield next(myiterator)
+
+
+def intertwined_sequences(myobject, generator_definitions={}):
+ iters = {}
+ for mydict in myobject:
+ if 'letters' in mydict:
+ iters['alph-lett'] = iter(alphabet(letters=mydict['letters']))
+ elif 'code' in mydict and mydict['code'] == 'bg':
+ iters['alph-bg'] = iter(alphabet(code='bg'))
+ elif 'code' in mydict and mydict['code'] == 'lat':
+ iters['alph-lat'] = iter(alphabet(code='lat'))
+ elif mydict['sequence'] == 'fibonacci':
+ iters['fibonacci-'] = iter(fibonacci())
+ elif mydict['sequence'] == 'primes':
+ iters['primes-'] = iter(primes())
+
+ for mydict in myobject:
+ if mydict['sequence'] in generator_definitions:
+ if mydict['sequence'] in iters:
+ myiter = iters[mydict['sequence']]
+ elif 'num' in mydict:
+ myiter = iter(
+ generator_definitions[mydict['sequence']](mydict['num']))
+ else:
+ myiter = iter(generator_definitions[mydict['sequence']]())
+ for _ in range(mydict['length']):
+ yield next(myiter)
+ iters[mydict['sequence']] = myiter
+ elif mydict['sequence'] == 'alphabet':
+ if 'letters' in mydict:
+ myalphabet = 'alph-lett'
+ myiter = iters['alph-lett']
+ elif 'code' in mydict and mydict['code'] == 'bg':
+ myalphabet = 'alph-bg'
+ myiter = iters['alph-bg']
+ elif 'code' in mydict and mydict['code'] == 'lat':
+ myalphabet = 'alph-lat'
+ myiter = iters['alph-lat']
+ for _ in range(mydict['length']):
+ yield next(myiter)
+ iters[myalphabet] = myiter
+ else:
+ if mydict['sequence'] == 'fibonacci':
+ myfunction = 'fibonacci-'
+ myiter = iters['fibonacci-']
+ elif mydict['sequence'] == 'primes':
+ myfunction = 'primes-'
+ myiter = iters['primes-']
+ for _ in range(mydict['length']):
+ yield next(myiter)
+ iters[myfunction] = myiter
myobject
(което ежду другото е доста кофти име) може да бъде безкрайна редица, което предполагам те подсеща защо е проблем да я обхождаш няколко пъти. :)