Решение на Генератори и итератори от Стоян Иванов

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

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

Резултати

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

Код

def fibonacci():
first = 1
second = 1
yield first
yield second
while True:
last = first
first = second
second = first + last
yield second
def is_prime(number):
return len([x for x in range(1, number) if number % x == 0]) == 1
def get_next_prime(number):
while True:
number += 1
if is_prime(number):
return number
def primes():
a = 2
yield a
while True:
a = get_next_prime(a)
yield a
def alphabet(code="", letters=""):
if letters != "":
i = 0
while i <= len(letters):
yield letters[i]
i += 1
else:
if code == "bg":
i = ord("а")
while i <= ord("я"):
yield chr(i)
i += 1
elif code == "lat":
i = ord("a")
while i <= ord("z"):
yield chr(i)
i += 1
else:
raise StopIteration
def intertwined_sequences(sequences, **kwargs):
fibonacci_generator = fibonacci()
primes_generator = primes()
alphabet_generator = alphabet()
for dictionary in sequences:
i = dictionary["length"]
if "letters" in dictionary:
alphabet_generator = alphabet(letters=dictionary["letters"])
elif "code" in dictionary:
alphabet_generator = alphabet(code=dictionary["code"])
sequence = dictionary["sequence"]
while i > 0:
if sequence == "fibonacci":
yield next(fibonacci_generator)
elif sequence == "primes":
yield next(primes_generator)
elif sequence == "alphabet":
yield next(alphabet_generator)
elif "generator_definitions" in kwargs and \
sequence in kwargs["generator_definitions"]:
generator_definitions = kwargs["generator_definitions"]
if len(dictionary) > 2:
for k, v in iter(dictionary.items()):
if k != 'sequence' and k != 'length':
gen = generator_definitions[sequence](v)
while i > 0:
yield next(gen)
i = i - 1
else:
gen = generator_definitions[sequence]()
while i > 0:
yield next(gen)
i = i - 1
else:
yield next(dictionary["sequence"])
i = i - 1

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

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

======================================================================
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_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

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

----------------------------------------------------------------------
Ran 14 tests in 16.145s

FAILED (errors=8)

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

Стоян обнови решението на 02.04.2015 01:48 (преди над 9 години)

+def fibonacci():
+ first = 1
+ second = 1
+ yield first
+ yield second
+ while True:
+ last = first
+ first = second
+ second = first + last
+ yield second
+
+
+def is_prime(number):
+ return len([x for x in range(1, number) if number % x == 0]) == 1
+
+
+def get_next_prime(number):
+ while True:
+ number += 1
+ if is_prime(number):
+ return number
+
+
+def primes():
+ a = 2
+ yield a
+ while True:
+ a = get_next_prime(a)
+ yield a
+
+
+def alphabet(code="", letters=""):
+ if letters != "":
+ i = 0
+ while i <= len(letters):
+ yield letters[i]
+ i += 1
+ else:
+ if code == "bg":
+ i = ord("а")
+ while i <= ord("я"):
+ yield chr(i)
+ i += 1
+ elif code == "lat":
+ i = ord("a")
+ while i <= ord("z"):
+ yield chr(i)
+ i += 1
+ else:
+ raise StopIteration
+
+
+def intertwined_sequences(sequences, **kwargs):
+ fibonacci_generator = fibonacci()
+ primes_generator = primes()
+ alphabet_generator = alphabet()
+ for dictionary in sequences:
+ i = dictionary["length"]
+ if "letters" in dictionary:
+ alphabet_generator = alphabet(letters=dictionary["letters"])
+ elif "code" in dictionary:
+ alphabet_generator = alphabet(code=dictionary["code"])
+ sequence = dictionary["sequence"]
+ while i > 0:
+ if sequence == "fibonacci":
+ yield next(fibonacci_generator)
+ elif sequence == "primes":
+ yield next(primes_generator)
+ elif sequence == "alphabet":
+ yield next(alphabet_generator)
+ elif "generator_definitions" in kwargs and \
+ sequence in kwargs["generator_definitions"]:
+ generator_definitions = kwargs["generator_definitions"]
+
+ if len(dictionary) > 2:
+ for k, v in iter(dictionary.items()):
+ if k != 'sequence' and k != 'length':
+ yield next(generator_definitions[sequence](v))
+ else:
+ yield next(generator_definitions[sequence]())
+ else:
+ yield next(dictionary["sequence"])
+ i = i - 1

Стоян обнови решението на 02.04.2015 20:44 (преди над 9 години)

def fibonacci():
first = 1
second = 1
yield first
yield second
while True:
last = first
first = second
second = first + last
yield second
def is_prime(number):
return len([x for x in range(1, number) if number % x == 0]) == 1
def get_next_prime(number):
while True:
number += 1
if is_prime(number):
return number
def primes():
a = 2
yield a
while True:
a = get_next_prime(a)
yield a
def alphabet(code="", letters=""):
if letters != "":
i = 0
while i <= len(letters):
yield letters[i]
i += 1
else:
if code == "bg":
i = ord("а")
while i <= ord("я"):
yield chr(i)
i += 1
elif code == "lat":
i = ord("a")
while i <= ord("z"):
yield chr(i)
i += 1
else:
raise StopIteration
def intertwined_sequences(sequences, **kwargs):
fibonacci_generator = fibonacci()
primes_generator = primes()
alphabet_generator = alphabet()
for dictionary in sequences:
i = dictionary["length"]
if "letters" in dictionary:
alphabet_generator = alphabet(letters=dictionary["letters"])
elif "code" in dictionary:
alphabet_generator = alphabet(code=dictionary["code"])
sequence = dictionary["sequence"]
while i > 0:
if sequence == "fibonacci":
yield next(fibonacci_generator)
elif sequence == "primes":
yield next(primes_generator)
elif sequence == "alphabet":
yield next(alphabet_generator)
elif "generator_definitions" in kwargs and \
sequence in kwargs["generator_definitions"]:
generator_definitions = kwargs["generator_definitions"]
if len(dictionary) > 2:
for k, v in iter(dictionary.items()):
if k != 'sequence' and k != 'length':
- yield next(generator_definitions[sequence](v))
+ gen = generator_definitions[sequence](v)
+ while i > 0:
+ yield next(gen)
+ i = i - 1
else:
- yield next(generator_definitions[sequence]())
+ gen = generator_definitions[sequence]()
+ while i > 0:
+ yield next(gen)
+ i = i - 1
else:
yield next(dictionary["sequence"])
i = i - 1