Решение на Генератори и итератори от Ивайло Караманолев

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

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

Резултати

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

Код

from itertools import count
import math
def fibonacci():
a = 1
b = 1
while True:
yield a
c = a + b
a = b
b = c
def is_prime(n):
if n % 2 == 0 and n > 2:
return False
for i in range(3, int(math.sqrt(n)) + 1, 2):
if n % i == 0:
return False
return True
def primes():
for i in count(2):
if is_prime(i):
yield i
def alphabet(**kwargs):
code = kwargs.get('code')
letters = kwargs.get('letters')
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for i in letters:
yield i
def intertwined_sequences(seqs, **kwargs):
generator_definitions = kwargs.get('generator_definitions') or {}
sources = {
'fibonacci': fibonacci(),
'primes': primes()
}
for name, generator_maker in generator_definitions.items():
sources[name] = generator_maker()
for seq in seqs:
source = sources.get(seq['sequence'])
if source is None:
if seq['sequence'] == 'alphabet':
maker = alphabet
else:
maker = generator_definitions[seq['sequence']]
kwargs = dict(seq)
del kwargs['sequence']
del kwargs['length']
source = maker(**kwargs)
sources[seq['sequence']] = source
for _ in range(seq['length']):
yield next(source)

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

......EE..E...
======================================================================
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 6.186s

FAILED (errors=3)

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

Ивайло обнови решението на 31.03.2015 03:06 (преди над 9 години)

+from itertools import count
+import math
+
+
+def fibonacci():
+ a = 1
+ b = 1
+ while True:
+ yield a
+ c = a + b
+ a = b
+ b = c
+
+
+def is_prime(n):
+ if n % 2 == 0 and n > 2:
+ return False
+ for i in range(3, int(math.sqrt(n)) + 1, 2):
+ if n % i == 0:
+ return False
+ return True
+
+
+def primes():
+ for i in count(2):
+ if is_prime(i):
+ yield i
+
+
+def alphabet(**kwargs):
+ code = kwargs.get('code')
+ letters = kwargs.get('letters')
+ if code == 'lat':
+ letters = 'abcdefghijklmnopqrstuvwxyz'
+ elif code == 'bg':
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for i in letters:
+ yield i
+
+
+def intertwined_sequences(seqs, **kwargs):
+ generator_definitions = kwargs.get('generator_definitions') or {}
+ sources = {
+ 'fibonacci': fibonacci(),
+ 'primes': primes()
+ }
+ for name, generator_maker in generator_definitions.items():
+ sources[name] = generator_maker()
+ for seq in seqs:
+ source = sources.get(seq['sequence'])
+ if source is None:
+ if seq['sequence'] == 'alphabet':
+ maker = alphabet
+ else:
+ maker = generator_definitions[seq['sequence']]
+ kwargs = dict(seq)
+ del kwargs['sequence']
+ del kwargs['length']
+ source = maker(**kwargs)
+ sources[seq['sequence']] = source
+ for _ in range(seq['length']):
+ yield next(source)