Решение на Генератори и итератори от Николай Георгиев

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

Към профила на Николай Георгиев

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 14 успешни тест(а)
  • 0 неуспешни тест(а)

Код

from collections import defaultdict
from itertools import count
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
def is_prime(n):
return not any(n % i == 0 for i in range(2, n))
return (prime for prime in count(2) if is_prime(prime))
def alphabet(code='', letters=''):
alphabets = defaultdict(lambda: letters,
{'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'})
alphabet = letters if letters else alphabets[code]
for letter in alphabet:
yield letter
def intertwined_sequences(iterable, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet})
generator_objects = {}
for item in iterable:
sequence = item['sequence']
length = item['length']
params = {}
for key in item.keys():
if key not in ['sequence', 'length']:
params[key] = item[key]
if sequence not in generator_objects.keys():
generator_objects[sequence] =\
iter(generator_definitions[sequence](**params))
for i in range(length):
yield next(generator_objects[sequence])

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

..............
----------------------------------------------------------------------
Ran 14 tests in 1.929s

OK

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

Николай обнови решението на 28.03.2015 22:13 (преди над 9 години)

+from collections import defaultdict
+from itertools import count
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ def is_prime(n):
+ return not any(n % i == 0 for i in range(2, n))
+ return (prime for prime in count(2) if is_prime(prime))
+
+
+def alphabet(code='', letters=''):
+ alphabets = defaultdict(lambda: letters,
+ {'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'})
+ alphabet = letters if letters else alphabets[code]
+
+ for letter in alphabet:
+ yield letter
+
+
+def intertwined_sequences(iterable, generator_definitions={}):
+ generator_definitions.update({'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet})
+ generator_objects = {}
+ for item in iterable:
+ sequence = item['sequence']
+ length = item['length']
+ params = {}
+ for key in item.keys():
+ if key not in ['sequence', 'length']:
+ params[key] = item[key]
+
+ if sequence not in generator_objects.keys():
+ generator_objects[sequence] = \
+ generator_definitions[sequence](**params)
+
+ for i in range(length):
+ yield next(generator_objects[sequence])

Николай обнови решението на 31.03.2015 19:13 (преди над 9 години)

from collections import defaultdict
from itertools import count
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
def is_prime(n):
return not any(n % i == 0 for i in range(2, n))
return (prime for prime in count(2) if is_prime(prime))
def alphabet(code='', letters=''):
alphabets = defaultdict(lambda: letters,
{'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'})
alphabet = letters if letters else alphabets[code]
for letter in alphabet:
yield letter
def intertwined_sequences(iterable, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet})
generator_objects = {}
for item in iterable:
sequence = item['sequence']
length = item['length']
params = {}
for key in item.keys():
if key not in ['sequence', 'length']:
params[key] = item[key]
if sequence not in generator_objects.keys():
- generator_objects[sequence] = \
- generator_definitions[sequence](**params)
+ generator_objects[sequence] =\
+ iter(generator_definitions[sequence](**params))
for i in range(length):
yield next(generator_objects[sequence])