Решение на Генератори и итератори от Красимир Атанасов

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

Към профила на Красимир Атанасов

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 12 успешни тест(а)
  • 2 неуспешни тест(а)

Код

def fibonacci():
yield 1
prev = 0
after = 1
while True:
result = prev + after
prev = after
after = result
yield result
def primes():
yield 2
yield 3
pl = [2, 3]
pp = 3
while True:
pp += 2
for i in pl:
if pp % i == 0:
break
else:
pl.append(pp)
yield pp
def alphabet(*, code='bg', letters=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
seq = ''
if len(letters) > 0:
seq = letters
else:
seq = alphabets[code]
for el in seq:
yield el
default_generator_definitions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def intertwined_sequences(specs, *, generator_definitions=None):
gen = None
it = None
gen_iterators = {}
specs_copy = specs[:]
if generator_definitions is None:
generator_definitions = default_generator_definitions
for spec in specs_copy:
gen_key = spec['sequence']
gen_length = spec['length']
if gen_key in gen_iterators:
it = gen_iterators[gen_key]
else:
del spec['sequence']
del spec['length']
func = generator_definitions[gen_key]
gen = func(**spec)
it = iter(gen)
gen_iterators[gen_key] = it
for _ in range(gen_length):
yield next(it)

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 4.380s

FAILED (errors=2)

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

Красимир обнови решението на 03.04.2015 16:43 (преди над 9 години)

+def fibonacci():
+ yield 1
+ prev = 0
+ after = 1
+ while True:
+ result = prev + after
+ prev = after
+ after = result
+ yield result
+
+
+def primes():
+ yield 2
+ yield 3
+ pl = [2, 3]
+ pp = 3
+ while True:
+ pp += 2
+ for i in pl:
+ if pp % i == 0:
+ break
+ else:
+ pl.append(pp)
+ yield pp
+
+
+def alphabet(*, code='bg', letters=''):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+
+ seq = ''
+ if len(letters) > 0:
+ seq = letters
+ else:
+ seq = alphabets[code]
+
+ for el in seq:
+ yield el
+
+
+default_generator_definitions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+}
+
+
+def intertwined_sequences(specs, *, generator_definitions=None):
+ gen = None
+ it = None
+ gen_iterators = {}
+ specs_copy = specs[:]
+
+ if generator_definitions is None:
+ generator_definitions = default_generator_definitions
+
+ for spec in specs_copy:
+ gen_key = spec['sequence']
+ gen_length = spec['length']
+ del spec['sequence']
+ del spec['length']
+
+ if gen_key in gen_iterators:
+ it = gen_iterators[gen_key]
+ else:
+ func = generator_definitions[gen_key]
+ #if len(spec) > 0:
+ gen = func(**spec)
+ #else:
+ #gen = func()
+ it = iter(gen)
+ gen_iterators[gen_key] = it
+
+ for _ in range(gen_length):
+ yield next(it)

Красимир обнови решението на 03.04.2015 16:48 (преди над 9 години)

def fibonacci():
yield 1
prev = 0
after = 1
while True:
result = prev + after
prev = after
after = result
yield result
def primes():
yield 2
yield 3
pl = [2, 3]
pp = 3
while True:
pp += 2
for i in pl:
if pp % i == 0:
break
else:
pl.append(pp)
yield pp
def alphabet(*, code='bg', letters=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
seq = ''
if len(letters) > 0:
seq = letters
else:
seq = alphabets[code]
for el in seq:
yield el
default_generator_definitions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def intertwined_sequences(specs, *, generator_definitions=None):
gen = None
it = None
gen_iterators = {}
specs_copy = specs[:]
if generator_definitions is None:
generator_definitions = default_generator_definitions
for spec in specs_copy:
gen_key = spec['sequence']
gen_length = spec['length']
- del spec['sequence']
- del spec['length']
if gen_key in gen_iterators:
it = gen_iterators[gen_key]
else:
+ del spec['sequence']
+ del spec['length']
func = generator_definitions[gen_key]
- #if len(spec) > 0:
gen = func(**spec)
- #else:
- #gen = func()
it = iter(gen)
gen_iterators[gen_key] = it
for _ in range(gen_length):
yield next(it)