Решение на Генератори и итератори от Галина Димитрова

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

Към профила на Галина Димитрова

Резултати

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

Код

from itertools import islice
def fibonacci():
first = 1
second = 1
while True:
yield first
first, second = second, first + second
def is_prime(n):
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(**kwargs):
for key, value in kwargs.items():
if key == "letters":
for char in islice(value, 0, None):
yield char
break
elif value == "bg" and "letters" not in kwargs.keys():
letters = "абвгдежзийклмнопрстуфхцчшщъьюя"
for char in islice(letters, 0, None):
yield char
elif value == "lat" and "letters" not in kwargs.keys():
letters = "abcdefghijklmnopqrstuvwxyz"
for char in islice(letters, 0, None):
yield char
def intertwined_sequences(seq, **kwargs):
fib_numbers = fibonacci()
prime_numbers = primes()
for item in seq:
length = item["length"]
if item["sequence"] == "fibonacci":
while length > 0:
yield next(fib_numbers)
length -= 1
elif item["sequence"] == "primes":
while length > 0:
yield next(prime_numbers)
length -= 1
elif item["sequence"] == "alphabet":
if "letters" in item.keys():
letters_char = alphabet(letters=item["letters"])
while length > 0:
yield next(letters_char)
length -= 1
else:
characters = alphabet(code=item["code"])
while length > 0:
yield next(characters)
length -= 1

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

......EE.EEE..
======================================================================
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 10.350s

FAILED (errors=5)

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

Галина обнови решението на 02.04.2015 23:18 (преди над 9 години)

+from itertools import islice
+
+
+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def is_prime(n):
+ for i in range(2, int(n ** 0.5) + 1):
+ if n % i == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(**kwargs):
+ for key, value in kwargs.items():
+ if key == "letters":
+ for char in islice(value, 0, None):
+ yield char
+ break
+ elif value == "bg" and "letters" not in kwargs.keys():
+ letters = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ for char in islice(letters, 0, None):
+ yield char
+ elif value == "lat" and "letters" not in kwargs.keys():
+ letters = "abcdefghijklmnopqrstuvwxyz"
+ for char in islice(letters, 0, None):
+ yield char
+
+
+def intertwined_sequences(seq, **kwargs):
+ fib_numbers = fibonacci()
+ prime_numbers = primes()
+ for item in seq:
+ length = item["length"]
+ if item["sequence"] == "fibonacci":
+ while length > 0:
+ yield next(fib_numbers)
+ length -= 1
+ elif item["sequence"] == "primes":
+ while length > 0:
+ yield next(prime_numbers)
+ length -= 1
+ elif item["sequence"] == "alphabet":
+ if "letters" in item.keys():
+ letters_char = alphabet(letters=item["letters"])
+ while length > 0:
+ yield next(letters_char)
+ length -= 1
+ else:
+ characters = alphabet(code=item["code"])
+ while length > 0:
+ yield next(characters)
+ length -= 1