Решение на Генератори и итератори от Клара Кайралах

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

Към профила на Клара Кайралах

Резултати

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

Код

import string
from itertools import islice
import itertools
from itertools import cycle
def primes():
first_prime_number = 2
primes = set()
while True:
for number in primes:
if first_prime_number % number == 0:
break
else:
primes.add(first_prime_number)
yield first_prime_number
first_prime_number += 1
def fibonacci():
first_number, second_number, counter = 1, 1, 0
while True:
yield first_number
first_number, second_number = second_number, first_number + \
second_number
counter += 1
def alphabet(*, code='', letters=''):
lat = list(string.ascii_lowercase)
bg = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
'з', 'и', 'й', 'к', 'л', 'м', 'н',
'о', 'п', 'р', 'с', 'т', 'у', 'ф',
'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
'ю', 'я']
if(code != '' and letters != ''):
index = 0
while True:
yield letters[index]
index = index + 1
if(code != ''):
if(code == 'lat'):
while True:
for letter in lat:
yield letter
if(code == 'bg'):
while True:
for l in bg:
yield l
if(letters != ''):
index = 0
while True:
yield letters[index]
index = index + 1
def intertwined_sequences(given_dict, *, generator_definitions=''):
return_list = []
fibonacci_numbers = fibonacci()
prime_numbers = primes()
alphabet_bg = alphabet(code='bg')
alphabet_lat = alphabet(code='lat')
generator_def = generator_definitions
for keys in given_dict:
len = keys.get('length')
sequence = keys.get('sequence')
code = keys.get('code')
letter = keys.get('letters')
if(sequence == 'fibonacci'):
return_list.append(list(islice(fibonacci_numbers, len)))
if(sequence == 'primes'):
return_list.append(list(islice(prime_numbers, len)))
if(sequence == 'alphabet'):
if(code == 'bg'):
return_list.append(list(islice(alphabet_bg, len)))
if(code == 'lat'):
return_list.append(list(islice(alphabet_lat, len)))
if(code is None):
alphabet_ = alphabet(letters=letter)
return_list.append(list(islice(alphabet_, len)))
if(sequence != 'fibonacci' and sequence != 'alphabet'
and sequence != 'primes'):
for key, value in generator_def.items():
if(sequence == key):
generator_function = value()
return_list.append(list(islice(generator_function, len)))
for item in list(itertools.chain(*return_list)):
yield item

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

.E....EE..EE..
======================================================================
ERROR: test_endless_letters_generator (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_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 10.352s

FAILED (errors=5)

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

Клара обнови решението на 03.04.2015 10:17 (преди над 9 години)

+import string
+from itertools import islice
+import itertools
+from itertools import cycle
+
+
+def primes():
+ first_prime_number = 2
+ primes = set()
+ while True:
+ for number in primes:
+ if first_prime_number % number == 0:
+ break
+ else:
+ primes.add(first_prime_number)
+ yield first_prime_number
+ first_prime_number += 1
+
+
+def fibonacci():
+ first_number, second_number, counter = 1, 1, 0
+ while True:
+ yield first_number
+ first_number, second_number = second_number, first_number + \
+ second_number
+ counter += 1
+
+
+def alphabet(*, code='', letters=''):
+ lat = list(string.ascii_lowercase)
+ bg = ['а', 'б', 'в', 'г', 'д', 'е', 'ж',
+ 'з', 'и', 'й', 'к', 'л', 'м', 'н',
+ 'о', 'п', 'р', 'с', 'т', 'у', 'ф',
+ 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь',
+ 'ю', 'я']
+
+ if(code != '' and letters != ''):
+ index = 0
+ while True:
+ yield letters[index]
+ index = index + 1
+
+ if(code != ''):
+ if(code == 'lat'):
+ while True:
+ for letter in lat:
+ yield letter
+ if(code == 'bg'):
+ while True:
+ for l in bg:
+ yield l
+ if(letters != ''):
+ index = 0
+ while True:
+ yield letters[index]
+ index = index + 1
+
+
+def intertwined_sequences(given_dict, *, generator_definitions=''):
+ return_list = []
+
+ fibonacci_numbers = fibonacci()
+ prime_numbers = primes()
+ alphabet_bg = alphabet(code='bg')
+ alphabet_lat = alphabet(code='lat')
+ generator_def = generator_definitions
+
+ for keys in given_dict:
+ len = keys.get('length')
+ sequence = keys.get('sequence')
+ code = keys.get('code')
+ letter = keys.get('letters')
+
+ if(sequence == 'fibonacci'):
+ return_list.append(list(islice(fibonacci_numbers, len)))
+
+ if(sequence == 'primes'):
+ return_list.append(list(islice(prime_numbers, len)))
+
+ if(sequence == 'alphabet'):
+ if(code == 'bg'):
+ return_list.append(list(islice(alphabet_bg, len)))
+
+ if(code == 'lat'):
+ return_list.append(list(islice(alphabet_lat, len)))
+
+ if(code is None):
+ alphabet_ = alphabet(letters=letter)
+ return_list.append(list(islice(alphabet_, len)))
+
+ if(sequence != 'fibonacci' and sequence != 'alphabet'
+ and sequence != 'primes'):
+ for key, value in generator_def.items():
+ if(sequence == key):
+ generator_function = value()
+ return_list.append(list(islice(generator_function, len)))
+
+ for item in list(itertools.chain(*return_list)):
+ yield item