Решение на Генератори и итератори от Йордан Михайлов

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

Към профила на Йордан Михайлов

Резултати

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

Код

def fibonacci():
current, next = 1, 1
while 1:
yield current
current, next = next, current + next
def isprime(number):
for devider in range(2, number):
if number % devider == 0:
return False
return True
def primes():
number = 2
while 1:
if isprime(number):
yield number
number = number + 1
def intertwined_sequences(items):
for generator in items:
if generator['sequence'] == 'fibonacci':
fibonacci_numbers = fibonacci()
for j in range(0, generator['length']):
yield next(fibonacci_numbers)
elif generator['sequence'] == 'primes':
prime_numbers = primes()
for j in range(0, generator['length']):
yield next(prime_numbers)
else:
if 'code' in generator:
alphabetical_order = alphabet(code=generator['code'])
else:
alphabetical_order = alphabet(letters=generator['letters'])
for j in range(0, generator['length']):
yield next(alphabetical_order)
def alphabet(*, letters='', code=''):
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
lat = 'abcdefghijklmnopqrstuwxyz'
alphabet = letters if letters else bg if code == 'bg' else lat
for letter in alphabet:
yield letter

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

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

======================================================================
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_intertwine_repeating_builtin (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

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

----------------------------------------------------------------------
Ran 14 tests in 13.458s

FAILED (errors=6)

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

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

+def fibonacci():
+ current, next = 1, 1
+ while 1:
+ yield current
+ current, next = next, current + next
+
+
+def isprime(number):
+ for devider in range(2, number):
+ if number % devider == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while 1:
+ if isprime(number):
+ yield number
+ number = number + 1
+
+
+def intertwined_sequences(items):
+ for generator in items:
+ if generator['sequence'] == 'fibonacci':
+ fibonacci_numbers = fibonacci()
+ for j in range(0, generator['length']):
+ yield next(fibonacci_numbers)
+ elif generator['sequence'] == 'primes':
+ prime_numbers = primes()
+ for j in range(0, generator['length']):
+ yield next(prime_numbers)
+ else:
+ if 'code' in generator:
+ alphabetical_order = alphabet(code=generator['code'])
+ else:
+ alphabetical_order = alphabet(letters=generator['letters'])
+ for j in range(0, generator['length']):
+ yield next(alphabetical_order)
+
+
+def alphabet(*, letters='', code=''):
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ lat = 'abcdefghijklmnopqrstuwxyz'
+ alphabet = letters if letters else bg if code == 'bg' else lat
+ for letter in alphabet:
+ yield letter