Решение на Генератори и итератори от Кристиан Ташков

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

Към профила на Кристиан Ташков

Резултати

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

Код

def fibonacci():
first = 1
second = 1
while True:
yield first
second += first
first = second - first
def primes():
number = 2
primes = []
while True:
if all((number % prime for prime in primes)):
primes.append(number)
yield number
number += 1
lat_alphabet = [chr(ord('a') + i) for i in range(26)]
bg_alphabet = [chr(ord('а') + i) for i in range(32) if i != 27 and i != 29]
def alphabet(code=None, letters=None):
if letters:
used_letters = letters
elif code == 'lat':
used_letters = lat_alphabet
else:
used_letters = bg_alphabet
return (letter for letter in used_letters)
def get_or_add_generator(generators, sequence, definitions):
generator_key = sequence['sequence']
generator = generators.get(generator_key, None)
if not generator:
generator_args = sequence.copy()
del generator_args['sequence']
del generator_args['length']
generator = definitions[generator_key](**generator_args)
generators[generator_key] = generator
return generator
def intertwined_sequences(sequences, generator_definitions={}):
definitions = {'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet}
definitions.update(generator_definitions)
generators = {}
for sequence in sequences:
generator = get_or_add_generator(generators,
sequence,
definitions)
for i in range(sequence['length']):
yield next(generator)
raise StopIteration()

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

......EE......
======================================================================
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

----------------------------------------------------------------------
Ran 14 tests in 4.432s

FAILED (errors=2)

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

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

+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ second += first
+ first = second - first
+
+
+def primes():
+ number = 2
+ primes = []
+ while True:
+ if all((number % prime for prime in primes)):
+ primes.append(number)
+ yield number
+ number += 1
+
+lat_alphabet = (chr(ord('a') + i) for i in range(26))
+bg_alphabet = (chr(ord('а') + i) for i in range(32) if i != 27 and i != 29)
+
+
+def alphabet(code=None, letters=None):
+ if letters:
+ used_letters = letters
+ elif code == 'lat':
+ used_letters = lat_alphabet
+ else:
+ used_letters = bg_alphabet
+
+ return (letter for letter in used_letters)
+
+
+def get_or_add_generator(generators, sequence, definitions):
+ generator_key = sequence['sequence']
+ generator = generators.get(generator_key, None)
+ if not generator:
+ generator_args = sequence.copy()
+ del generator_args['sequence']
+ del generator_args['length']
+
+ generator = definitions[generator_key](**generator_args)
+ generators[generator_key] = generator
+ return generator
+
+
+base_definitions = {'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet}
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ definitions = base_definitions.copy()
+ definitions.update(generator_definitions)
+ generators = {}
+ for sequence in sequences:
+ generator = get_or_add_generator(generators,
+ sequence,
+ definitions)
+ for i in range(sequence['length']):
+ yield next(generator)
+
+ raise StopIteration()

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

def fibonacci():
first = 1
second = 1
while True:
yield first
second += first
first = second - first
def primes():
number = 2
primes = []
while True:
if all((number % prime for prime in primes)):
primes.append(number)
yield number
number += 1
-lat_alphabet = (chr(ord('a') + i) for i in range(26))
-bg_alphabet = (chr(ord('а') + i) for i in range(32) if i != 27 and i != 29)
+lat_alphabet = [chr(ord('a') + i) for i in range(26)]
+bg_alphabet = [chr(ord('а') + i) for i in range(32) if i != 27 and i != 29]
def alphabet(code=None, letters=None):
if letters:
used_letters = letters
elif code == 'lat':
used_letters = lat_alphabet
else:
used_letters = bg_alphabet
return (letter for letter in used_letters)
def get_or_add_generator(generators, sequence, definitions):
generator_key = sequence['sequence']
generator = generators.get(generator_key, None)
if not generator:
generator_args = sequence.copy()
del generator_args['sequence']
del generator_args['length']
+ generator = definitions[generator_key](**generator_args)
+ generators[generator_key] = generator
- generator = definitions[generator_key](**generator_args)
- generators[generator_key] = generator
return generator
-base_definitions = {'fibonacci': fibonacci,
- 'primes': primes,
- 'alphabet': alphabet}
-
-
def intertwined_sequences(sequences, generator_definitions={}):
- definitions = base_definitions.copy()
+ definitions = {'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet}
definitions.update(generator_definitions)
generators = {}
+
for sequence in sequences:
generator = get_or_add_generator(generators,
sequence,
definitions)
- for i in range(sequence['length']):
- yield next(generator)
+ for i in range(sequence['length']):
+ yield next(generator)
raise StopIteration()