Решение на Генератори и итератори от Тачо Желев

Обратно към всички решения

Към профила на Тачо Желев

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 10 успешни тест(а)
  • 4 неуспешни тест(а)

Код

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(sequences, generator_definitions={}):
iters = {}
for mydict in sequences:
if 'letters' in mydict and 'alph-lett' not in iters:
iters['alph-lett'] = iter(alphabet(letters=mydict['letters']))
elif 'code' in mydict and mydict['code'] == 'bg' \
and 'alph-bg' not in iters:
iters['alph-bg'] = iter(alphabet(code='bg'))
elif 'code' in mydict and mydict['code'] == 'lat' \
and 'alph-lat' not in iters:
iters['alph-lat'] = iter(alphabet(code='lat'))
elif mydict['sequence'] == 'fibonacci':
iters['fibonacci-'] = iter(fibonacci())
elif mydict['sequence'] == 'primes':
iters['primes-'] = iter(primes())
if 'alph-lett' in iters and \
('alph-bg' in iters or 'alph-lat' in iters):
for seq, myiter in iters.items():
if seq == 'alph-bg' or seq == 'alph-lat':
iters[seq] = iters['alph-lett']
for mydict in sequences:
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

Лог от изпълнението

.......E..EEE.
======================================================================
ERROR: test_infinite_intertwined (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_kwargs_generator (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_repeating_with_different_args (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_for_larger_prime (test.TestPrimes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 8.232s

FAILED (errors=4)

История (3 версии и 2 коментара)

Тачо обнови решението на 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

Тачо обнови решението на 03.04.2015 15:37 (преди над 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:
+ if 'letters' in mydict and 'alph-lett' not in iters:
iters['alph-lett'] = iter(alphabet(letters=mydict['letters']))
- elif 'code' in mydict and mydict['code'] == 'bg':
+ elif 'code' in mydict and mydict['code'] == 'bg' \
+ and 'alph-bg' not in iters:
iters['alph-bg'] = iter(alphabet(code='bg'))
- elif 'code' in mydict and mydict['code'] == 'lat':
+ elif 'code' in mydict and mydict['code'] == 'lat' \
+ and 'alph-lat' not in iters:
iters['alph-lat'] = iter(alphabet(code='lat'))
elif mydict['sequence'] == 'fibonacci':
iters['fibonacci-'] = iter(fibonacci())
elif mydict['sequence'] == 'primes':
iters['primes-'] = iter(primes())
+ if 'alph-lett' in iters and \
+ ('alph-bg' in iters or 'alph-lat' in iters):
+ for seq, myiter in iters.items():
+ if seq == 'alph-bg' or seq == 'alph-lat':
+ iters[seq] = iters['alph-lett']
+
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

Тачо обнови решението на 03.04.2015 15:47 (преди над 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={}):
+def intertwined_sequences(sequences, generator_definitions={}):
iters = {}
- for mydict in myobject:
+ for mydict in sequences:
if 'letters' in mydict and 'alph-lett' not in iters:
iters['alph-lett'] = iter(alphabet(letters=mydict['letters']))
elif 'code' in mydict and mydict['code'] == 'bg' \
and 'alph-bg' not in iters:
iters['alph-bg'] = iter(alphabet(code='bg'))
elif 'code' in mydict and mydict['code'] == 'lat' \
and 'alph-lat' not in iters:
iters['alph-lat'] = iter(alphabet(code='lat'))
elif mydict['sequence'] == 'fibonacci':
iters['fibonacci-'] = iter(fibonacci())
elif mydict['sequence'] == 'primes':
iters['primes-'] = iter(primes())
if 'alph-lett' in iters and \
('alph-bg' in iters or 'alph-lat' in iters):
for seq, myiter in iters.items():
if seq == 'alph-bg' or seq == 'alph-lat':
iters[seq] = iters['alph-lett']
- for mydict in myobject:
+ for mydict in sequences:
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