Решение на Генератори и итератори от Цветан Иванов

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

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

Резултати

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

Код

import math
BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LAT_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
LATIN_ALPHABET_CODE = 'lat'
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def get_aphabet_by_code(code):
if code == LATIN_ALPHABET_CODE:
return LAT_ALPHABET
return BG_ALPHABET
def alphabet(code='', letters=''):
if not letters:
letters = get_aphabet_by_code(code)
for letter in letters:
yield letter
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def generator_factory(generator):
sequence = generator['sequence']
generator_copy = generator.copy()
generator_copy.pop('sequence')
generator_copy.pop('length')
return iter(GENERATORS[sequence](**generator_copy))
def intertwined_sequences(generators, generator_definitions={}):
current_generators = {}
GENERATORS.update(generator_definitions)
for generator in generators:
sequence = generator['sequence']
if sequence not in current_generators:
current_generators[sequence] = generator_factory(generator)
for x in range(generator['length']):
yield next(current_generators[sequence])
return current_generators

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

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

FAILED (errors=1)

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

Цветан обнови решението на 03.04.2015 09:01 (преди над 9 години)

+import math
+
+BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+LAT_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
+LATIN_ALPHABET_CODE = 'lat'
+
+
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ if number > 1:
+ if number == 2:
+ return True
+ if number % 2 == 0:
+ return False
+ for current in range(3, int(math.sqrt(number) + 1), 2):
+ if number % current == 0:
+ return False
+ return True
+ return False
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def get_aphabet_by_code(code):
+ if code == LATIN_ALPHABET_CODE:
+ return LAT_ALPHABET
+
+ return BG_ALPHABET
+
+
+def alphabet(code='', letters=''):
+ if not letters:
+ letters = get_aphabet_by_code(code)
+
+ for letter in letters:
+ yield letter
+
+
+GENERATORS = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+}
+
+
+def generator_factory(generator):
+ sequence = generator['sequence']
+
+ generator_copy = generator.copy()
+
+ generator_copy.pop('sequence')
+ generator_copy.pop('length')
+
+ return GENERATORS[sequence](**generator_copy)
+
+
+def intertwined_sequences(generators, generator_definitions={}):
+ current_generators = {}
+ GENERATORS.update(generator_definitions)
+ for generator in generators:
+ sequence = generator['sequence']
+ if sequence not in current_generators:
+ current_generators[sequence] = generator_factory(generator)
+
+ for x in range(generator['length']):
+ yield next(current_generators[sequence])
+
+ return current_generators

Цветан обнови решението на 03.04.2015 14:07 (преди над 9 години)

import math
BG_ALPHABET = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LAT_ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
LATIN_ALPHABET_CODE = 'lat'
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def get_aphabet_by_code(code):
if code == LATIN_ALPHABET_CODE:
return LAT_ALPHABET
return BG_ALPHABET
def alphabet(code='', letters=''):
if not letters:
letters = get_aphabet_by_code(code)
for letter in letters:
yield letter
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def generator_factory(generator):
sequence = generator['sequence']
generator_copy = generator.copy()
generator_copy.pop('sequence')
generator_copy.pop('length')
- return GENERATORS[sequence](**generator_copy)
+ return iter(GENERATORS[sequence](**generator_copy))
def intertwined_sequences(generators, generator_definitions={}):
current_generators = {}
GENERATORS.update(generator_definitions)
for generator in generators:
sequence = generator['sequence']
if sequence not in current_generators:
current_generators[sequence] = generator_factory(generator)
for x in range(generator['length']):
yield next(current_generators[sequence])
return current_generators