Решение на Генератори и итератори от Александър Ваканин

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

Към профила на Александър Ваканин

Резултати

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

Код

LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
a = b = 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
for divider in range(2, int(number / 2) + 1):
if not number % divider:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(*, code='xx', letters=''):
if not len(letters):
if code == 'lat':
letters = LATIN_ALPHABET
else:
letters = BULGARIAN_ALPHABET
for letter in letters:
yield letter
def intertwined_sequences(input_generators, *, generator_definitions={}):
generators = {}
for generator in input_generators:
sequence = generator['sequence']
length = generator['length']
if sequence not in generators:
if sequence == 'fibonacci':
generators[sequence] = fibonacci()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'primes':
generators[sequence] = primes()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'alphabet':
if 'letters' in generator:
generators[sequence] = alphabet(
letters=generator['letters'])
for _ in range(length):
yield next(generators[sequence])
else:
generators[sequence] = alphabet(code=generator['code'])
for _ in range(length):
yield next(generators[sequence])
else:
del generator['sequence']
del generator['length']
args = generator
gen_func = iter(generator_definitions[sequence](**args))
generators[sequence] = gen_func
for _ in range(length):
yield next(generators[sequence])
else:
for _ in range(length):
yield next(generators[sequence])

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

.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

----------------------------------------------------------------------
Ran 14 tests in 3.033s

FAILED (errors=1)

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

Александър обнови решението на 01.04.2015 13:52 (преди над 9 години)

+LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
+BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+def fibonacci():
+ a = b = 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ for divider in range(2, int(number / 2) + 1):
+ if not number % divider:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(code='xx', letters=''):
+ if not len(letters):
+ if code == 'lat':
+ letters = LATIN_ALPHABET
+ else:
+ letters = BULGARIAN_ALPHABET
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(input_generators, generator_definitions={}):
+ generators = {}
+ for generator in input_generators:
+ sequence = generator['sequence']
+ length = generator['length']
+ if sequence not in generators:
+ if sequence == 'fibonacci':
+ generators[sequence] = fibonacci()
+ for _ in range(length):
+ yield next(generators[sequence])
+ elif sequence == 'primes':
+ generators[sequence] = primes()
+ for _ in range(length):
+ yield next(generators[sequence])
+
+ elif sequence == 'alphabet':
+ if 'letters' in generator:
+ generators[sequence] = alphabet(
+ letters=generator['letters'])
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ generators[sequence] = alphabet(code=generator['code'])
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ generator.pop('sequence')
+ generator.pop('length')
+ kwargs = generator
+ gen_func = iter(generator_definitions[sequence](**kwargs))
+ generators[sequence] = gen_func
+ for _ in range(length):
+ yield next(generators[sequence])
+ else:
+ for _ in range(length):
+ yield next(generators[sequence])

Александър обнови решението на 03.04.2015 13:39 (преди над 9 години)

LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
a = b = 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
for divider in range(2, int(number / 2) + 1):
if not number % divider:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
-def alphabet(code='xx', letters=''):
+def alphabet(*, code='xx', letters=''):
if not len(letters):
if code == 'lat':
letters = LATIN_ALPHABET
else:
letters = BULGARIAN_ALPHABET
for letter in letters:
yield letter
-def intertwined_sequences(input_generators, generator_definitions={}):
+def intertwined_sequences(input_generators, *, generator_definitions = {}):
generators = {}
for generator in input_generators:
sequence = generator['sequence']
length = generator['length']
if sequence not in generators:
if sequence == 'fibonacci':
generators[sequence] = fibonacci()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'primes':
generators[sequence] = primes()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'alphabet':
if 'letters' in generator:
generators[sequence] = alphabet(
letters=generator['letters'])
for _ in range(length):
yield next(generators[sequence])
else:
generators[sequence] = alphabet(code=generator['code'])
for _ in range(length):
yield next(generators[sequence])
else:
- generator.pop('sequence')
- generator.pop('length')
- kwargs = generator
- gen_func = iter(generator_definitions[sequence](**kwargs))
+ del generator['sequence']
+ del generator['length']
+ args = generator
+ gen_func = iter(generator_definitions[sequence](**args))
generators[sequence] = gen_func
for _ in range(length):
yield next(generators[sequence])
else:
for _ in range(length):
yield next(generators[sequence])

Александър обнови решението на 03.04.2015 13:41 (преди над 9 години)

LATIN_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
BULGARIAN_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
a = b = 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
for divider in range(2, int(number / 2) + 1):
if not number % divider:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(*, code='xx', letters=''):
if not len(letters):
if code == 'lat':
letters = LATIN_ALPHABET
else:
letters = BULGARIAN_ALPHABET
for letter in letters:
yield letter
-def intertwined_sequences(input_generators, *, generator_definitions = {}):
+def intertwined_sequences(input_generators, *, generator_definitions={}):
generators = {}
for generator in input_generators:
sequence = generator['sequence']
length = generator['length']
if sequence not in generators:
if sequence == 'fibonacci':
generators[sequence] = fibonacci()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'primes':
generators[sequence] = primes()
for _ in range(length):
yield next(generators[sequence])
elif sequence == 'alphabet':
if 'letters' in generator:
generators[sequence] = alphabet(
letters=generator['letters'])
for _ in range(length):
yield next(generators[sequence])
else:
generators[sequence] = alphabet(code=generator['code'])
for _ in range(length):
yield next(generators[sequence])
else:
del generator['sequence']
del generator['length']
args = generator
gen_func = iter(generator_definitions[sequence](**args))
generators[sequence] = gen_func
for _ in range(length):
yield next(generators[sequence])
else:
for _ in range(length):
yield next(generators[sequence])