Решение на Генератори и итератори от Станислав Траилов

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

Към профила на Станислав Траилов

Резултати

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

Код

def fibonacci():
a, b = 1, 1
while 1:
yield a
a, b = b, a + b
def is_prime(n):
for i in range (2, n-1):
if n % i == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
latin_alphabet ='abcdefghijklmnopqrstuvwxyz'
bulgarian_alphabet ='абвгдежзийклмнопрстуфьцчшщъьюя'
def alphabet(code='', letters=''):
if letters:
for letter in letters:
yield letter
elif code == 'bg':
for letter in bulgarian_alphabet:
yield letter
elif code == 'lat':
for letter in latin_alphabet:
yield letter
raise StopIteration
def intertwined_sequences(iter_object, my_generator={}):
fibonacci_generator = fibonacci()
primes_generator = primes()
for my_dict in iter_object:
seq = my_dict['sequence']
length = my_dict['length']
if seq == 'fibonacci':
i = 1
while i <= length:
yield next(fibonacci_generator)
i += 1
if seq == 'primes':
i = 1
while i <= length:
yield next(primes_generator)
i += 1
if seq == 'alphabet':
alphabet_generator = alphabet(code = my_dict['code'])
i = 1
while i <= length:
yield next(alphabet_generator)
i += 1

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

E.....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_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 14.302s

FAILED (errors=7)

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

Станислав обнови решението на 03.04.2015 12:45 (преди над 9 години)

+def fibonacci():
+ a, b = 1, 1
+ while 1:
+ yield a
+ a, b = b, a + b
+
+def is_prime(n):
+ for i in range (2, n-1):
+ if n % i == 0:
+ return False
+ return True
+
+def primes():
+ number = 2
+ while True:
+
+ if is_prime(number):
+ yield number
+ number += 1
+
+latin_alphabet ='abcdefghijklmnopqrstuvwxyz'
+
+bulgarian_alphabet ='абвгдежзийклмнопрстуфьцчшщъьюя'
+
+def alphabet(code='', letters=''):
+ if letters:
+ for letter in letters:
+ yield letter
+ elif code == 'bg':
+ for letter in bulgarian_alphabet:
+ yield letter
+ elif code == 'lat':
+ for letter in latin_alphabet:
+ yield letter
+ raise StopIteration

Станислав обнови решението на 03.04.2015 12:55 (преди над 9 години)

def fibonacci():
a, b = 1, 1
while 1:
yield a
a, b = b, a + b
def is_prime(n):
for i in range (2, n-1):
if n % i == 0:
return False
return True
def primes():
number = 2
while True:
-
if is_prime(number):
yield number
number += 1
latin_alphabet ='abcdefghijklmnopqrstuvwxyz'
bulgarian_alphabet ='абвгдежзийклмнопрстуфьцчшщъьюя'
def alphabet(code='', letters=''):
if letters:
for letter in letters:
yield letter
elif code == 'bg':
for letter in bulgarian_alphabet:
yield letter
elif code == 'lat':
for letter in latin_alphabet:
yield letter
raise StopIteration

Станислав обнови решението на 03.04.2015 14:40 (преди над 9 години)

def fibonacci():
a, b = 1, 1
while 1:
yield a
a, b = b, a + b
def is_prime(n):
for i in range (2, n-1):
if n % i == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
latin_alphabet ='abcdefghijklmnopqrstuvwxyz'
bulgarian_alphabet ='абвгдежзийклмнопрстуфьцчшщъьюя'
def alphabet(code='', letters=''):
if letters:
for letter in letters:
yield letter
elif code == 'bg':
for letter in bulgarian_alphabet:
yield letter
elif code == 'lat':
for letter in latin_alphabet:
yield letter
- raise StopIteration
+ raise StopIteration
+
+def intertwined_sequences(iter_object, my_generator={}):
+ fibonacci_generator = fibonacci()
+ primes_generator = primes()
+ for my_dict in iter_object:
+ seq = my_dict['sequence']
+ length = my_dict['length']
+ if seq == 'fibonacci':
+ i = 1
+ while i <= length:
+ yield next(fibonacci_generator)
+ i += 1
+ if seq == 'primes':
+ i = 1
+ while i <= length:
+ yield next(primes_generator)
+ i += 1
+ if seq == 'alphabet':
+ alphabet_generator = alphabet(code = my_dict['code'])
+ i = 1
+ while i <= length:
+ yield next(alphabet_generator)
+ i += 1