Решение на Генератори и итератори от Георги Мавридис

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

Към профила на Георги Мавридис

Резултати

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

Код

def fibonacci():
a, b = 0, 1
while True:
yield b
a, b = b, a + b
raise StopIteration
def sum_of_divisors(n):
return sum([x for x in range(1, n + 1) if n % x == 0])
def is_prime(n):
return sum_of_divisors(n) == n + 1
def next_prime(n):
n += 1
while not is_prime(n):
n += 1
return n
def primes():
curr = 2
while True:
yield curr
curr = next_prime(curr)
raise StopIteration
LATIN = list(map(chr, range(ord('a'), ord('z') + 1)))
CYRILLIC = list(map(chr, range(ord('а'), ord('я') + 1)))
def alphabet(code=None, letters=None):
if code == 'lat' and letters is None:
iterator = iter(LATIN)
while True:
letter = next(iterator)
yield letter
raise StopIteration
if code == 'bg' and letters is None:
iterator = iter(CYRILLIC)
while True:
letter = next(iterator)
yield letter
raise StopIteration
elif code is not None or letters is not None:
iterator = iter(letters)
while True:
letter = next(iterator)
yield letter
raise StopIteration

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

E.....EEEEEEE.
======================================================================
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_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 (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.246s

FAILED (errors=8)

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

Георги обнови решението на 03.04.2015 10:44 (преди над 9 години)

+def fibonacci():
+ a, b = 0, 1
+ while True:
+ yield b
+ a, b = b, a + b
+ raise StopIteration
+
+
+def sum_of_divisors(n):
+ return sum([x for x in range(1, n + 1) if n % x == 0])
+
+
+def is_prime(n):
+ return sum_of_divisors(n) == n + 1
+
+
+def next_prime(n):
+ n += 1
+ while not is_prime(n):
+ n += 1
+ return n
+
+
+def primes():
+ curr = 2
+ while True:
+ yield curr
+ curr = next_prime(curr)
+ raise StopIteration
+
+LATIN = list(map(chr, range(ord('a'), ord('z') + 1)))
+CYRILLIC = list(map(chr, range(ord('а'), ord('я') + 1)))
+
+
+def alphabet(code=None, letters=None):
+ if code == 'lat' and letters is None:
+ iterator = iter(LATIN)
+ while True:
+ letter = next(iterator)
+ yield letter
+ raise StopIteration
+ if code == 'bg' and letters is None:
+ iterator = iter(CYRILLIC)
+ while True:
+ letter = next(iterator)
+ yield letter
+ raise StopIteration
+ elif code is not None or letters is not None:
+ iterator = iter(letters)
+ while True:
+ letter = next(iterator)
+ yield letter
+ raise StopIteration