Решение на Генератори и итератори от София Петрова

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

Към профила на София Петрова

Резултати

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

Код

def fibonacci():
first = 1
second = 1
yield first
while True:
yield second
first, second = second, first + second
def primes():
first = 2
while True:
if is_prime(first):
yield first
first += 1
def is_prime(number):
for n in range(2, number):
if number % n == 0:
return False
return True
def alphabet(code='bg', letters=None):
if letters is None:
alphabets = {
'lat': [chr(element) for element in range(ord('a'), ord('z') + 1)],
'bg': [chr(element) for element in range(ord('а'), ord('я') + 1)
if chr(element) not in 'ыэ']
}
for letter in alphabets[code]:
yield letter
else:
for letter in letters:
yield letter
def intertwined_sequences(iterable, generator_definitions=None):
generator_functions = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
if generator_definitions is not None:
generator_functions.update(generator_definitions)
generators = {}
for item in iterable:
if not item['sequence'] in generators:
keyword_args = {key: value
for key, value in item.items()
if key not in ['sequence', 'length']}
generator = generator_functions[item['sequence']](**keyword_args)
generators[item['sequence']] = generator
for _ in range(item['length']):
yield next(generators[item['sequence']])

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

......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 5.684s

FAILED (errors=2)

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

София обнови решението на 03.04.2015 16:19 (преди над 9 години)

+def fibonacci():
+ first = 1
+ second = 1
+ yield first
+ while True:
+ yield second
+ first, second = second, first + second
+
+
+def primes():
+ first = 2
+ while True:
+ if is_prime(first):
+ yield first
+ first += 1
+
+
+def is_prime(number):
+ for n in range(2, number):
+ if number % n == 0:
+ return False
+ return True
+
+
+def alphabet(code='bg', letters=None):
+ if letters is None:
+ alphabets = {
+ 'lat': [chr(element) for element in range(ord('a'), ord('z') + 1)],
+ 'bg': [chr(element) for element in range(ord('а'), ord('я') + 1)
+ if chr(element) not in 'ыэ']
+ }
+ for letter in alphabets[code]:
+ yield letter
+ else:
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(iterable, generator_definitions=None):
+ generator_functions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+ if generator_definitions is not None:
+ generator_functions.update(generator_definitions)
+ generators = {}
+ for item in iterable:
+ if not item['sequence'] in generators:
+ keyword_args = {key: value
+ for key, value in item.items()
+ if key not in ['sequence', 'length']}
+ generator = generator_functions[item['sequence']](**keyword_args)
+ generators[item['sequence']] = generator
+ for _ in range(item['length']):
+ yield next(generators[item['sequence']])