Решение на Генератори и итератори от Марина Узунова

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

Към профила на Марина Узунова

Резултати

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

Код

def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
n = 2
primes = set()
while True:
for p in primes:
if n > 2 and n % 2 == 0:
break
if n % p == 0:
break
else:
primes.add(n)
yield n
n += 1
alphabets = {
'lat': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
'bg': ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и',
'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с',
'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
}
def alphabet(code='', letters=[]):
if len(letters) > 0:
alphabet = iter(letters)
else:
alphabet = iter(alphabets[code])
return alphabet
def intertwined_sequences(series, generator_definitions={}):
generators = {}
for element in series:
if element['sequence'] not in generators:
if element['sequence'] in generator_definitions:
gen = generator_definitions[element['sequence']]()
elif element['sequence'] == 'alphabet':
if 'letters' in element:
gen = alphabet(letters=element['letters'])
else:
gen = alphabet(code=element['code'])
else:
gen_name = element['sequence']
gen = globals()[gen_name]()
generators[element['sequence']] = gen
else:
gen = generators[element['sequence']]
for i in list(range(element['length'])):
yield next(gen)

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

.E....EE..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_generator_definitions (test.TestIntertwine)
----------------------------------------------------------------------
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

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

----------------------------------------------------------------------
Ran 14 tests in 8.574s

FAILED (errors=4)

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

Марина обнови решението на 03.04.2015 16:55 (преди над 9 години)

+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ n = 2
+ primes = set()
+ while True:
+ for p in primes:
+ if n > 2 and n % 2 == 0:
+ break
+ if n % p == 0:
+ break
+ else:
+ primes.add(n)
+ yield n
+ n += 1
+
+
+alphabets = {
+ 'lat': ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
+ 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
+ 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
+ 'bg': ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и',
+ 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с',
+ 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+}
+
+
+def alphabet(code='', letters=[]):
+ if len(letters) > 0:
+ alphabet = iter(letters)
+ else:
+ alphabet = iter(alphabets[code])
+ return alphabet
+
+
+def intertwined_sequences(series, generator_definitions={}):
+ generators = {}
+ for element in series:
+ if element['sequence'] not in generators:
+ if element['sequence'] in generator_definitions:
+ gen = generator_definitions[element['sequence']]()
+ elif element['sequence'] == 'alphabet':
+ if 'letters' in element:
+ gen = alphabet(letters=element['letters'])
+ else:
+ gen = alphabet(code=element['code'])
+ else:
+ gen_name = element['sequence']
+ gen = globals()[gen_name]()
+ generators[element['sequence']] = gen
+ else:
+ gen = generators[element['sequence']]
+ for i in list(range(element['length'])):
+ yield next(gen)
+