Решение на Генератори и итератори от Елица Илиева

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

Към профила на Елица Илиева

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 8 успешни тест(а)
  • 6 неуспешни тест(а)

Код

import itertools
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def is_prime(n):
if n == 2 or n == 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def primes():
a = 2
while True:
if is_prime(a):
yield a
a += 1
def alphabet(code='', letters=''):
alphabets = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
letters = letters if len(letters) else alphabets[code]
for character in letters:
yield str(character)
def intertwined_sequences(sequences, generator_definitions={
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}):
sequences = list(sequences)
for mapper in generator_definitions:
for seq in sequences:
if (seq['sequence'] != mapper):
continue
args = [item[1] for item in seq.items() if
item[0] not in ['length', 'sequence']]
if (args):
generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']](*args)
else:
generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']]()
try:
result = itertools.chain(list(itertools.chain((list(
itertools.islice(generator_definitions[seq['sequence']], 0, seq['length']))
for seq in sequences))))
return list(itertools.chain(*result))
except StopIteration:
pass

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

.E....EE.EEE..
======================================================================
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_intertwine_repeating_builtin (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

----------------------------------------------------------------------
Ran 14 tests in 12.218s

FAILED (errors=6)

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

Елица обнови решението на 03.04.2015 16:54 (преди над 9 години)

+import itertools
+
+
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(n):
+ if n == 2 or n == 3:
+ return True
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def primes():
+ a = 2
+ while True:
+ if is_prime(a):
+ yield a
+ a += 1
+
+
+def alphabet(code='', letters=''):
+ alphabets = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ }
+ letters = letters if len(letters) else alphabets[code]
+ for character in letters:
+ yield str(character)
+
+
+def intertwined_sequences(sequences, generator_definitions={
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }):
+ sequences = list(sequences)
+ for mapper in generator_definitions:
+ for seq in sequences:
+ if (seq['sequence'] != mapper):
+ continue
+ args = [item[1] for item in seq.items() if
+ item[0] not in ['length', 'sequence']]
+ if (args):
+ generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']](*args)
+ else:
+ generator_definitions[seq['sequence']] = generator_definitions[seq['sequence']]()
+ try:
+ result = itertools.chain(list(itertools.chain((list(
+ itertools.islice(generator_definitions[seq['sequence']], 0, seq['length']))
+ for seq in sequences))))
+ return list(itertools.chain(*result))
+ except StopIteration:
+ pass