Решение на Генератори и итератори от Георги Димов

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

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

Резултати

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

Код

from math import sqrt
def fibonacci():
current_member, next_member = 1, 1
while True:
yield current_member
current_member, next_member = next_member, current_member + next_member
def primes():
def is_prime(number):
for i in range(2, int(sqrt(number)) + 1):
if number % i == 0:
return False
return True
current_number = 2
while True:
yield current_number
current_number += 1
while not is_prime(current_number):
current_number += 1
def alphabet(code='', letters=''):
if letters == '':
alphabets = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'
}
letters = alphabets[code]
for letter in letters:
yield letter
def intertwined_sequences(descriptors, generator_definitions={}):
sequence_generators = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
sequence_generators.update(generator_definitions)
generators = {}
for sequence in descriptors:
name = sequence.pop('sequence')
length = sequence.pop('length')
if name not in generators:
generators[name] = iter(sequence_generators[name](**sequence))
while length > 0:
yield next(generators[name])
length -= 1

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

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

FAILED (errors=1)

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

Георги обнови решението на 03.04.2015 12:46 (преди над 9 години)

+from math import sqrt
+
+
+def fibonacci():
+ current_member, next_member = 1, 1
+
+ while True:
+ yield current_member
+ current_member, next_member = next_member, current_member + next_member
+
+
+def primes():
+ def is_prime(number):
+ for i in range(2, int(sqrt(number)) + 1):
+ if number % i == 0:
+ return False
+
+ return True
+
+ current_number = 2
+
+ while True:
+ yield current_number
+ current_number += 1
+
+ while not is_prime(current_number):
+ current_number += 1
+
+
+def alphabet(code='', letters=''):
+ if letters == '':
+ alphabets = {
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': 'abcdefghijklmnopqrstuvwxyz'
+ }
+
+ letters = alphabets[code]
+
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(descriptors, generator_definitions={}):
+ sequence_generators = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+
+ sequence_generators.update(generator_definitions)
+
+ generators = {}
+ for sequence in descriptors:
+ name = sequence.pop('sequence')
+ length = sequence.pop('length')
+
+ if name not in generators:
+ generators[name] = iter(sequence_generators[name](**sequence))
+
+ while length > 0:
+ yield next(generators[name])
+ length -= 1