Решение на Генератори и итератори от Людмил Делчев

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

Към профила на Людмил Делчев

Резултати

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

Код

import math
import string
from copy import copy
from itertools import count, chain
ALPHABETS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': string.ascii_lowercase}
def fibonacci():
current, next = 1, 1
while True:
yield current
current, next = next, current + next
def prime(number):
for divisor in range(2, int(math.sqrt(number)) + 1):
if number % divisor == 0:
return False
return True
def primes():
return (number for number in chain([2], count(3, 2)) if prime(number))
def alphabet(code=None, letters=None):
if letters:
for letter in letters:
yield letter
else:
for letter in ALPHABETS[code]:
yield letter
GENERATORS = {'fibonacci': fibonacci, 'primes': primes, 'alphabet': alphabet}
def intertwined_sequences(sequences, generator_definitions={}):
generator_definitions.update(GENERATORS)
used_generators = {}
for sequence in sequences:
sequence_name = sequence['sequence']
if sequence_name not in used_generators:
generator_args = copy(sequence)
del generator_args['sequence'], generator_args['length']
new_gen = generator_definitions[sequence_name](**generator_args)
used_generators[sequence_name] = iter(new_gen)
for _ in range(sequence['length']):
yield next(used_generators[sequence_name])

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

..............
----------------------------------------------------------------------
Ran 14 tests in 0.196s

OK

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

Людмил обнови решението на 31.03.2015 16:31 (преди над 9 години)

+import math
+import string
+from copy import copy
+from itertools import count, chain
+
+
+ALPHABETS = {
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': string.ascii_lowercase}
+
+
+def fibonacci():
+ current, next = 1, 1
+ while True:
+ yield current
+ current, next = next, current + next
+
+
+def prime(number):
+ for divisor in range(2, int(math.sqrt(number)) + 1):
+ if number % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ return (number for number in chain([2], count(3, 2)) if prime(number))
+
+
+def alphabet(code=None, letters=None):
+ if letters:
+ for letter in letters:
+ yield letter
+ else:
+ for letter in ALPHABETS[code]:
+ yield letter
+
+
+GENERATORS = {'fibonacci': fibonacci, 'primes': primes, 'alphabet': alphabet}
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ generator_definitions.update(GENERATORS)
+ used_generators = {}
+ for sequence in sequences:
+ sequence_name = sequence['sequence']
+ if sequence_name not in used_generators:
+ generator_args = copy(sequence)
+ del generator_args['sequence'], generator_args['length']
+ new_gen = generator_definitions[sequence_name](**generator_args)
+ used_generators[sequence_name] = new_gen
+ for _ in range(sequence['length']):
+ yield next(used_generators[sequence_name])

Людмил обнови решението на 31.03.2015 16:51 (преди над 9 години)

import math
import string
from copy import copy
from itertools import count, chain
ALPHABETS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': string.ascii_lowercase}
def fibonacci():
current, next = 1, 1
while True:
yield current
current, next = next, current + next
def prime(number):
for divisor in range(2, int(math.sqrt(number)) + 1):
if number % divisor == 0:
return False
return True
def primes():
return (number for number in chain([2], count(3, 2)) if prime(number))
def alphabet(code=None, letters=None):
if letters:
for letter in letters:
yield letter
else:
for letter in ALPHABETS[code]:
yield letter
GENERATORS = {'fibonacci': fibonacci, 'primes': primes, 'alphabet': alphabet}
def intertwined_sequences(sequences, generator_definitions={}):
generator_definitions.update(GENERATORS)
used_generators = {}
for sequence in sequences:
sequence_name = sequence['sequence']
if sequence_name not in used_generators:
generator_args = copy(sequence)
del generator_args['sequence'], generator_args['length']
new_gen = generator_definitions[sequence_name](**generator_args)
- used_generators[sequence_name] = new_gen
+ used_generators[sequence_name] = iter(new_gen)
for _ in range(sequence['length']):
yield next(used_generators[sequence_name])