Решение на Генератори и итератори от Светлин Василев

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

Към профила на Светлин Василев

Резултати

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

Код

def fibonacci():
start_elem, next_elem = 0, 1
while True:
yield next_elem
start_elem, next_elem = next_elem, start_elem + next_elem
raise StopIteration
def primes():
elem = 1
while True:
elem += 1
if all(elem % x != 0 for x in range(elem) if x != 1 and
x != elem and x != 0):
yield elem
raise StopIteration
def alphabet(**kwargs):
lat = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for key, value in kwargs.items():
if key is 'letters':
for letter in value:
yield letter
break
if kwargs['code'] is 'lat':
for letter in lat:
yield letter
else:
for letter in bg:
yield letter
raise StopIteration
def intertwined_sequences(sequence, **kwargs):
fib = fibonacci()
prime = primes()
for seq in sequence:
if seq['sequence'] is 'fibonacci':
while seq['length'] > 0:
seq['length'] -= 1
yield next(fib)
elif seq['sequence'] is 'primes':
while seq['length'] > 0:
seq['length'] -= 1
yield next(prime)
elif seq['sequence'] is 'alphabet':
if all(seq['sequence'] is not previous['sequence']
for previous in sequence[0:sequence.index(seq)]):
args = {key: value for (key, value) in seq.items()
if key != 'sequence' and key != 'length'}
alpha = (lambda x: alphabet(**x))
alpha_func = alpha(args)
while seq['length'] > 0:
seq['length'] -= 1
yield next(alpha_func)
else:
if all(seq['sequence'] is not previous['sequence']
for previous in sequence[0:sequence.index(seq)]):
arguments = {key: value for (key, value) in seq.items()
if key != 'sequence' and key != 'length'}
func = (lambda x:
kwargs['generator_definitions'][seq['sequence']](**x))
kwargs['generator_definitions'][seq['sequence']] = (
iter(func(arguments)))
while seq['length'] > 0:
seq['length'] -= 1
yield next(kwargs['generator_definitions'][seq['sequence']])
raise StopIteration

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

.......E....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

======================================================================
ERROR: test_for_larger_prime (test.TestPrimes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 4.190s

FAILED (errors=2)

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

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

+def fibonacci():
+ start_elem, next_elem = 0, 1
+ while True:
+ yield next_elem
+ start_elem, next_elem = next_elem, start_elem + next_elem
+ raise StopIteration
+
+
+def primes():
+ elem = 1
+ while True:
+ elem += 1
+ if all(elem % x != 0 for x in range(elem) if x != 1 and
+ x != elem and x != 0):
+ yield elem
+ raise StopIteration
+
+
+def alphabet(**kwargs):
+ lat = 'abcdefghijklmnopqrstuvwxyz'
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ for key, value in kwargs.items():
+ if key is 'letters':
+ for letter in value:
+ yield letter
+ break
+ if kwargs['code'] is 'lat':
+ for letter in lat:
+ yield letter
+ else:
+ for letter in bg:
+ yield letter
+ raise StopIteration
+
+
+def intertwined_sequences(sequence, **kwargs):
+ fib = fibonacci()
+ prime = primes()
+ for seq in sequence:
+ if seq['sequence'] is 'fibonacci':
+ while seq['length'] > 0:
+ seq['length'] -= 1
+ yield next(fib)
+ elif seq['sequence'] is 'primes':
+ while seq['length'] > 0:
+ seq['length'] -= 1
+ yield next(prime)
+ elif seq['sequence'] is 'alphabet':
+ if all(seq['sequence'] is not previous['sequence']
+ for previous in sequence[0:sequence.index(seq)]):
+ args = {key: value for (key, value) in seq.items()
+ if key != 'sequence' and key != 'length'}
+ alpha = (lambda x: alphabet(**x))
+ alpha_func = alpha(args)
+ while seq['length'] > 0:
+ seq['length'] -= 1
+ yield next(alpha_func)
+ else:
+ if all(seq['sequence'] is not previous['sequence']
+ for previous in sequence[0:sequence.index(seq)]):
+ arguments = {key: value for (key, value) in seq.items()
+ if key != 'sequence' and key != 'length'}
+ func = (lambda x:
+ kwargs['generator_definitions'][seq['sequence']](**x))
+ kwargs['generator_definitions'][seq['sequence']] = (
+ func(arguments))
+ while seq['length'] > 0:
+ seq['length'] -= 1
+ yield next(kwargs['generator_definitions'][seq['sequence']])
+ raise StopIteration

Светлин обнови решението на 31.03.2015 15:44 (преди над 9 години)

def fibonacci():
start_elem, next_elem = 0, 1
while True:
yield next_elem
start_elem, next_elem = next_elem, start_elem + next_elem
raise StopIteration
def primes():
elem = 1
while True:
elem += 1
if all(elem % x != 0 for x in range(elem) if x != 1 and
x != elem and x != 0):
yield elem
raise StopIteration
def alphabet(**kwargs):
lat = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for key, value in kwargs.items():
if key is 'letters':
for letter in value:
yield letter
break
if kwargs['code'] is 'lat':
for letter in lat:
yield letter
else:
for letter in bg:
yield letter
raise StopIteration
def intertwined_sequences(sequence, **kwargs):
fib = fibonacci()
prime = primes()
for seq in sequence:
if seq['sequence'] is 'fibonacci':
while seq['length'] > 0:
seq['length'] -= 1
yield next(fib)
elif seq['sequence'] is 'primes':
while seq['length'] > 0:
seq['length'] -= 1
yield next(prime)
elif seq['sequence'] is 'alphabet':
if all(seq['sequence'] is not previous['sequence']
for previous in sequence[0:sequence.index(seq)]):
args = {key: value for (key, value) in seq.items()
if key != 'sequence' and key != 'length'}
alpha = (lambda x: alphabet(**x))
alpha_func = alpha(args)
while seq['length'] > 0:
seq['length'] -= 1
yield next(alpha_func)
else:
if all(seq['sequence'] is not previous['sequence']
for previous in sequence[0:sequence.index(seq)]):
arguments = {key: value for (key, value) in seq.items()
if key != 'sequence' and key != 'length'}
func = (lambda x:
kwargs['generator_definitions'][seq['sequence']](**x))
kwargs['generator_definitions'][seq['sequence']] = (
- func(arguments))
+ iter(func(arguments)))
while seq['length'] > 0:
seq['length'] -= 1
yield next(kwargs['generator_definitions'][seq['sequence']])
raise StopIteration