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

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

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

Резултати

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

Код

def get_fibonacci_number(n):
if n <= 2:
return 1
else:
return get_fibonacci_number(n - 1) + get_fibonacci_number(n - 2)
def fibonacci():
current_element = 1
while True:
yield get_fibonacci_number(current_element)
current_element += 1
def is_prime(number):
if number < 2:
return False
elif number == 2:
return True
for divisor in range(2, int(number ** 0.5) + 1):
if number % divisor == 0:
return False
return True
def primes():
current_prime = 2
while True:
if is_prime(current_prime):
yield current_prime
current_prime += 1
def alphabet(*, code="", letters=""):
alphabet = iter(letters)
flag = False
for letter in alphabet:
flag = True
yield letter
if flag:
raise StopIteration
if code == "bg":
alphabet = iter(["а", "б", "в", "г", "д", "е", "ж", "з", "и",
"й", "к", "л", "м", "н", "о", "п", "р", "с",
"т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ",
"ь", "ю", "я"])
while True:
yield next(alphabet)
elif code == "lat":
alphabet = map(chr, range(97, 123))
while True:
yield next(alphabet)
def intertwined_sequences(sequences, *, generator_definitions={}):
defined_generators = {}
for key, value in generator_definitions.items():
generator = value()
defined_generators[key] = generator
fibonacci_numbers = fibonacci()
prime_numbers = primes()
for current_sequence in sequences:
if current_sequence["sequence"] == "fibonacci":
for index in range(current_sequence["length"]):
yield next(fibonacci_numbers)
elif current_sequence["sequence"] == "primes":
for index in range(current_sequence["length"]):
yield next(prime_numbers)
elif current_sequence["sequence"] == "alphabet":
alph_code = ""
alph_letters = ""
if "code" in current_sequence:
alph_code = current_sequence["code"]
else:
alph_letters = current_sequence["letters"]
generator = alphabet(code = alph_code, letters = alph_letters)
for index in range(current_sequence["length"]):
yield next(generator)
else:
generator = current_sequence["sequence"]
for index in range(current_sequence["length"]):
yield next(defined_generators[generator])

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

.....EEE.EEE..
======================================================================
ERROR: test_longer_sequence (test.TestFibonacci)
----------------------------------------------------------------------
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 12.291s

FAILED (errors=6)

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

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

+def get_fibonacci_number(n):
+ if n <= 2:
+ return 1
+ else:
+ return get_fibonacci_number(n - 1) + get_fibonacci_number(n - 2)
+
+
+def fibonacci():
+ current_element = 1
+ while True:
+ yield get_fibonacci_number(current_element)
+ current_element += 1
+
+
+def is_prime(number):
+ if number < 2:
+ return False
+ elif number == 2:
+ return True
+ for divisor in range(2, int(number ** 0.5) + 1):
+ if number % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ current_prime = 2
+ while True:
+ if is_prime(current_prime):
+ yield current_prime
+ current_prime += 1
+
+
+def alphabet(*, code="", letters=""):
+ alphabet = iter(letters)
+ flag = False
+ for letter in alphabet:
+ flag = True
+ yield letter
+ if flag:
+ raise StopIteration
+ if code == "bg":
+ alphabet = iter(["а", "б", "в", "г", "д", "е", "ж", "з", "и",
+ "й", "к", "л", "м", "н", "о", "п", "р", "с",
+ "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ",
+ "ь", "ю", "я"])
+ while True:
+ yield next(alphabet)
+ elif code == "lat":
+ alphabet = map(chr, range(97, 123))
+ while True:
+ yield next(alphabet)
+
+
+def intertwined_sequences(sequences, *, generator_definitions={}):
+ defined_generators = {}
+ for key, value in generator_definitions.items():
+ generator = value()
+ defined_generators[key] = generator
+
+ fibonacci_numbers = fibonacci()
+ prime_numbers = primes()
+ for current_sequence in sequences:
+ if current_sequence["sequence"] == "fibonacci":
+ for index in range(current_sequence["length"]):
+ yield next(fibonacci_numbers)
+ elif current_sequence["sequence"] == "primes":
+ for index in range(current_sequence["length"]):
+ yield next(prime_numbers)
+ elif current_sequence["sequence"] == "alphabet":
+ alph_code = ""
+ alph_letters = ""
+ if "code" in current_sequence:
+ alph_code = current_sequence["code"]
+ else:
+ alph_letters = current_sequence["letters"]
+ generator = alphabet(code = alph_code, letters = alph_letters)
+ for index in range(current_sequence["length"]):
+ yield next(generator)
+ else:
+ generator = current_sequence["sequence"]
+ for index in range(current_sequence["length"]):
+ yield next(defined_generators[generator])