Решение на Генератори и итератори от Теодор Климентов

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

Към профила на Теодор Климентов

Резултати

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

Код

from math import floor, sqrt
def fibonacci():
a, b = 0, 1
yield b
while True:
a, b = b, a + b
yield b
def primes():
n = 2
yield n
while True:
n += 1
for i in range(2, floor(sqrt(n)) + 1):
if n % i == 0:
break
else:
yield n
def alphabet(code=None, letters=None):
built_in = {'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'}
if not letters:
letters = built_in[code]
return (letter for letter in letters)
def intertwined_sequences(sequences, generator_definitions={}):
existing_generators = {}
generator_funcs = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
generator_funcs.update(generator_definitions)
fixed_keys = ('sequence', 'length')
for sequence in sequences:
name = sequence['sequence']
if name in existing_generators:
generator = existing_generators[name]
else:
args = {i: sequence[i] for i in sequence if i not in fixed_keys}
generator = iter(generator_funcs[name](**args))
existing_generators[name] = generator
for _ in range(sequence['length']):
yield next(generator)

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

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

FAILED (errors=1)

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

Теодор обнови решението на 30.03.2015 01:10 (преди над 9 години)

+from math import floor, sqrt
+
+
+def fibonacci():
+ a, b = 0, 1
+ yield b
+ while True:
+ a, b = b, a + b
+ yield b
+
+
+def primes():
+ n = 2
+ yield n
+ while True:
+ n += 1
+ for i in range(2, floor(sqrt(n)) + 1):
+ if n % i == 0:
+ break
+ else:
+ yield n
+
+
+def alphabet(code=None, letters=None):
+ built_in = {'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': 'abcdefghijklmnopqrstuvwxyz'}
+ if not letters:
+ letters = built_in[code]
+ return (letter for letter in letters)
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ existing_generators = {}
+ generator_definitions.update(globals())
+ fixed_keys = ('sequence', 'length')
+
+ for sequence in sequences:
+ name = sequence['sequence']
+ if name in existing_generators:
+ generator = existing_generators[name]
+ else:
+ args = {i: sequence[i] for i in sequence if i not in fixed_keys}
+ generator = generator_definitions[name](**args)
+ existing_generators[name] = generator
+ for _ in range(sequence['length']):
+ yield next(generator)

Теодор обнови решението на 30.03.2015 23:55 (преди над 9 години)

from math import floor, sqrt
def fibonacci():
a, b = 0, 1
yield b
while True:
a, b = b, a + b
yield b
def primes():
n = 2
yield n
while True:
n += 1
for i in range(2, floor(sqrt(n)) + 1):
if n % i == 0:
break
else:
yield n
def alphabet(code=None, letters=None):
built_in = {'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'}
if not letters:
letters = built_in[code]
return (letter for letter in letters)
def intertwined_sequences(sequences, generator_definitions={}):
existing_generators = {}
- generator_definitions.update(globals())
+ generator_funcs = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+ generator_funcs.update(generator_definitions)
fixed_keys = ('sequence', 'length')
for sequence in sequences:
name = sequence['sequence']
if name in existing_generators:
generator = existing_generators[name]
else:
args = {i: sequence[i] for i in sequence if i not in fixed_keys}
- generator = generator_definitions[name](**args)
+ generator = generator_funcs[name](**args)
existing_generators[name] = generator
for _ in range(sequence['length']):
yield next(generator)

Теодор обнови решението на 31.03.2015 19:46 (преди над 9 години)

from math import floor, sqrt
def fibonacci():
a, b = 0, 1
yield b
while True:
a, b = b, a + b
yield b
def primes():
n = 2
yield n
while True:
n += 1
for i in range(2, floor(sqrt(n)) + 1):
if n % i == 0:
break
else:
yield n
def alphabet(code=None, letters=None):
built_in = {'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'}
if not letters:
letters = built_in[code]
return (letter for letter in letters)
def intertwined_sequences(sequences, generator_definitions={}):
existing_generators = {}
generator_funcs = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
generator_funcs.update(generator_definitions)
fixed_keys = ('sequence', 'length')
for sequence in sequences:
name = sequence['sequence']
if name in existing_generators:
generator = existing_generators[name]
else:
args = {i: sequence[i] for i in sequence if i not in fixed_keys}
- generator = generator_funcs[name](**args)
+ generator = iter(generator_funcs[name](**args))
existing_generators[name] = generator
for _ in range(sequence['length']):
yield next(generator)