Решение на Генератори и итератори от Ангел Ангелов

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

Към профила на Ангел Ангелов

Резултати

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

Код

from math import sqrt
from itertools import count
def fibonacci():
a, b = 0, 1
while True:
a, b = b, a + b
yield a
def is_prime(number):
if number == 1:
return False
if number == 2:
return True
return all(number % divisor for divisor in range(2, int(sqrt(number)) + 2))
def primes():
for number in count(1):
if is_prime(number):
yield number
def alphabet(*, code=None, letters=""):
if not letters:
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for letter in letters:
yield letter
def intertwined_sequences(sequences, *, generator_definitions={}):
sequence_functions = {
'alphabet': alphabet,
'primes': primes,
'fibonacci': fibonacci
}
sequence_functions.update(generator_definitions)
sequence_iterators = {}
for sequence in sequences:
sequence_arguments = dict(sequence)
sequence_name = sequence_arguments.pop('sequence')
length = sequence_arguments.pop('length')
if sequence_name not in sequence_iterators:
sequence_iterators[sequence_name] = sequence_functions[
sequence_name
](**sequence_arguments)
iterator = sequence_iterators[sequence_name]
for _ in range(length):
yield next(iterator)

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

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

FAILED (errors=2)

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

Ангел обнови решението на 29.03.2015 23:12 (преди над 9 години)

+from math import sqrt
+from itertools import count
+
+
+def fibonacci():
+ a, b = 0, 1
+ while True:
+ a, b = b, a + b
+ yield a
+
+
+def is_prime(number):
+ if number == 1:
+ return False
+ if number == 2:
+ return True
+ return all(number % divisor for divisor in range(2, int(sqrt(number)) + 2))
+
+
+def primes():
+ return (number for number in count(1) if is_prime(number))
+
+
+def alphabet(*, code=None, letters=""):
+ if not letters:
+ if code == 'lat':
+ letters = 'abcdefghijklmnopqrstuvwxyz'
+ elif code == 'bg':
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ return (letter for letter in letters)
+
+
+def intertwined_sequences(sequences, *, generator_definitions={}):
+ sequence_functions = {
+ 'alphabet': alphabet,
+ 'primes': primes,
+ 'fibonacci': fibonacci
+ }
+ sequence_functions.update(generator_definitions)
+
+ sequence_iterators = {}
+ for sequence in sequences:
+ sequence_arguments = dict(sequence)
+
+ sequence_name = sequence_arguments.pop('sequence')
+ length = sequence_arguments.pop('length')
+
+ if sequence_name not in sequence_iterators:
+ sequence_iterators[sequence_name] = sequence_functions[
+ sequence_name
+ ](**sequence_arguments)
+
+ iterator = sequence_iterators[sequence_name]
+
+ for _ in range(length):
+ yield next(iterator)

Ангел обнови решението на 31.03.2015 23:08 (преди над 9 години)

from math import sqrt
from itertools import count
def fibonacci():
a, b = 0, 1
while True:
a, b = b, a + b
yield a
def is_prime(number):
if number == 1:
return False
if number == 2:
return True
return all(number % divisor for divisor in range(2, int(sqrt(number)) + 2))
def primes():
- return (number for number in count(1) if is_prime(number))
+ for number in count(1):
+ if is_prime(number):
+ yield number
def alphabet(*, code=None, letters=""):
if not letters:
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
- return (letter for letter in letters)
+ for letter in letters:
+ yield letter
def intertwined_sequences(sequences, *, generator_definitions={}):
sequence_functions = {
'alphabet': alphabet,
'primes': primes,
'fibonacci': fibonacci
}
sequence_functions.update(generator_definitions)
sequence_iterators = {}
for sequence in sequences:
sequence_arguments = dict(sequence)
sequence_name = sequence_arguments.pop('sequence')
length = sequence_arguments.pop('length')
if sequence_name not in sequence_iterators:
sequence_iterators[sequence_name] = sequence_functions[
sequence_name
](**sequence_arguments)
iterator = sequence_iterators[sequence_name]
for _ in range(length):
yield next(iterator)