Решение на Генератори и итератори от Спасимир Нонев

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

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

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 11 успешни тест(а)
  • 3 неуспешни тест(а)

Код

import itertools
import math
def fibonacci():
a = b = 1
while True:
yield a
a, b = b, a + b
def primes():
return (i for i in itertools.count(2)
if all(i % t for t in range(2, int(math.sqrt(i)) + 1)))
def alphabet(**kwargs):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if 'letters' in kwargs:
for item in kwargs['letters']:
yield item
elif 'code' in kwargs:
if kwargs['code'] == 'lat':
for item in latin:
yield item
elif kwargs['code'] == 'bg':
for item in bg:
yield item
def intertwined_sequences(composition, generator_definitions={}):
sequence_fibonacci = fibonacci()
sequence_primes = primes()
for item in composition:
if item['sequence'] == 'alphabet':
if 'letters' in item:
sequence_alphabet = alphabet(letters=item['letters'])
elif 'code' in item:
sequence_alphabet = alphabet(code=item['code'])
break
for item in composition:
if item['sequence'] == 'fibonacci':
for i in range(item['length']):
yield next(sequence_fibonacci)
elif item['sequence'] == 'primes':
for i in range(item['length']):
yield next(sequence_primes)
elif item['sequence'] == 'alphabet':
for i in range(item['length']):
yield next(sequence_alphabet)
elif generator_definitions:
for key, value in generator_definitions.items():
if item['sequence'] == key:
sequence_gen_def = value()
for i in range(item['length']):
yield next(sequence_gen_def)

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

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

======================================================================
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_kwargs_generator (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 6.231s

FAILED (errors=3)

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

Спасимир обнови решението на 03.04.2015 15:59 (преди над 9 години)

+import itertools
+
+import math
+
+
+def fibonacci():
+ a = b = 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ return (i for i in itertools.count(2)
+ if all(i % t for t in range(2, int(math.sqrt(i)) + 1)))
+
+
+def alphabet(**kwargs):
+ latin = 'abcdefghijklmnopqrstuvwxyz'
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ if 'letters' in kwargs:
+ for item in kwargs['letters']:
+ yield item
+ elif 'code' in kwargs:
+ if kwargs['code'] == 'lat':
+ for item in latin:
+ yield item
+ elif kwargs['code'] == 'bg':
+ for item in bg:
+ yield item
+
+
+def intertwined_sequences(composition, generator_definitions={}):
+ sequence1 = fibonacci()
+ sequence2 = primes()
+ for item in composition:
+ if item['sequence'] == 'alphabet':
+ if 'letters' in item:
+ sequence3 = alphabet(letters=item['letters'])
+ elif 'code' in item:
+ sequence3 = alphabet(code=item['code'])
+ break
+ for item in composition:
+ if item['sequence'] == 'fibonacci':
+ for i in range(item['length']):
+ yield next(sequence1)
+ elif item['sequence'] == 'primes':
+ for i in range(item['length']):
+ yield next(sequence2)
+ elif item['sequence'] == 'alphabet':
+ for i in range(item['length']):
+ yield next(sequence3)
+ elif generator_definitions:
+ for key, value in generator_definitions.items():
+ if item['sequence'] == key:
+ sequence4 = value()
+ for i in range(item['length']):
+ yield next(sequence4)
+

Спасимир обнови решението на 03.04.2015 16:35 (преди над 9 години)

import itertools
import math
def fibonacci():
a = b = 1
while True:
yield a
a, b = b, a + b
def primes():
return (i for i in itertools.count(2)
if all(i % t for t in range(2, int(math.sqrt(i)) + 1)))
def alphabet(**kwargs):
latin = 'abcdefghijklmnopqrstuvwxyz'
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if 'letters' in kwargs:
for item in kwargs['letters']:
yield item
elif 'code' in kwargs:
if kwargs['code'] == 'lat':
for item in latin:
yield item
elif kwargs['code'] == 'bg':
for item in bg:
yield item
def intertwined_sequences(composition, generator_definitions={}):
- sequence1 = fibonacci()
- sequence2 = primes()
+ sequence_fibonacci = fibonacci()
+ sequence_primes = primes()
for item in composition:
if item['sequence'] == 'alphabet':
if 'letters' in item:
- sequence3 = alphabet(letters=item['letters'])
+ sequence_alphabet = alphabet(letters=item['letters'])
elif 'code' in item:
- sequence3 = alphabet(code=item['code'])
+ sequence_alphabet = alphabet(code=item['code'])
break
for item in composition:
if item['sequence'] == 'fibonacci':
for i in range(item['length']):
- yield next(sequence1)
+ yield next(sequence_fibonacci)
elif item['sequence'] == 'primes':
for i in range(item['length']):
- yield next(sequence2)
+ yield next(sequence_primes)
elif item['sequence'] == 'alphabet':
for i in range(item['length']):
- yield next(sequence3)
+ yield next(sequence_alphabet)
elif generator_definitions:
for key, value in generator_definitions.items():
if item['sequence'] == key:
- sequence4 = value()
+ sequence_gen_def = value()
for i in range(item['length']):
- yield next(sequence4)
+ yield next(sequence_gen_def)