Решение на Генератори и итератори от Никола Терзиев

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

Към профила на Никола Терзиев

Резултати

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

Код

from argparse import ArgumentError
def fibonacci():
prev, nxt = 1, 1
while True:
yield prev
prev, nxt = nxt, prev + nxt
def primes():
number = 1
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(*, code='lat', letters=False):
lat_letters = 'abcdefghijklmnopqrstuvwxyz'
bg_letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
used_letters = ''
if letters is not False:
used_letters = letters
elif code == 'lat':
used_letters = lat_letters
elif code == 'bg':
used_letters = bg_letters
else:
raise ArgumentError
for letter in used_letters:
yield letter
def intertwined_sequences(generators, *, generator_definitions={}):
iterators = {}
for generator in generators:
if generator['sequence'] not in iterators.keys():
iterator_args = {}
for seq_arg in generator:
if seq_arg is not 'sequence' and seq_arg is not 'length':
iterator_args.update({seq_arg: generator[seq_arg]})
if generator['sequence'] in generator_definitions:
generated_iterator = generator_definitions[generator['sequence']](**iterator_args)
else:
generated_iterator = globals()[generator['sequence']](**iterator_args)
iterators.update({generator['sequence']: generated_iterator})
for iteration in range(generator['length']):
yield next(iterators[generator['sequence']])

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

......EE......
======================================================================
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

----------------------------------------------------------------------
Ran 14 tests in 4.222s

FAILED (errors=2)

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

Никола обнови решението на 02.04.2015 22:36 (преди над 9 години)

+from argparse import ArgumentError
+
+
+def fibonacci():
+ prev, nxt = 1, 1
+
+ while True:
+ yield prev
+ prev, nxt = nxt, prev + nxt
+
+
+def primes():
+ number = 1
+
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def is_prime(n):
+ if n <= 3:
+ return n >= 2
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def alphabet(*, code='lat', letters=False):
+ lat_letters = 'abcdefghijklmnopqrstuvwxyz'
+ bg_letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ used_letters = ''
+ if letters is not False:
+ used_letters = letters
+ elif code == 'lat':
+ used_letters = lat_letters
+ elif code == 'bg':
+ used_letters = bg_letters
+ else:
+ raise ArgumentError
+
+ for letter in used_letters:
+ yield letter
+
+
+def intertwined_sequences(generators, *, generator_definitions={}):
+ iterators = {}
+
+ for generator in generators:
+ if generator['sequence'] not in iterators.keys():
+ iterator_args = {}
+
+ for seq_arg in generator:
+ if seq_arg is not 'sequence' and seq_arg is not 'length':
+ iterator_args.update({seq_arg: generator[seq_arg]})
+
+ if generator['sequence'] in generator_definitions:
+ generated_iterator = generator_definitions[generator['sequence']](**iterator_args)
+ else:
+ generated_iterator = globals()[generator['sequence']](**iterator_args)
+ iterators.update({generator['sequence']: generated_iterator})
+
+ for iteration in range(generator['length']):
+ yield next(iterators[generator['sequence']])