Решение на Генератори и итератори от Емине Башева

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

Към профила на Емине Башева

Резултати

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

Код

from itertools import islice
def fibonacci(a=1, b=1):
while True:
yield a
next_fib = a + b
a = b
b = next_fib
def is_prime(num):
for j in range(2, num):
if (num % j) == 0:
return False
return True
def primes(i=2):
while True:
if is_prime(i):
yield i
i += 1
def alphabet(code='', letters=''):
alph = ''
if code == 'lat' and letters == '':
alph = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg' and letters == '':
alph = 'абвгдежзийклмнопрстуфхцчшщъьюя'
else:
alph = letters
for i in range(len(alph)):
yield alph[i]
def intertwined_sequences(sequences=()):
for i in range(len(sequences)):
seq = sequences[i]['sequence']
length = sequences[i]['length']
result = []
if seq == 'fibonacci':
result.extend(list(islice(fibonacci(), length)))
elif seq == 'primes':
result.extend(list(islice(primes(), length)))
elif seq == 'alphabet':
seq_code = sequences[i]['code']
result.extend(list(islice(alphabet(code=seq_code), length)))
for j in range(len(result)):
yield result[j]

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

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

----------------------------------------------------------------------
Ran 14 tests in 13.545s

FAILED (errors=6)

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

Емине обнови решението на 03.04.2015 02:58 (преди над 9 години)

+from itertools import islice
+
+
+def fibonacci(a=1, b=1):
+ while True:
+ yield a
+ next_fib = a + b
+ a = b
+ b = next_fib
+
+
+def is_prime(num):
+ for j in range(2, num):
+ if (num % j) == 0:
+ return False
+ return True
+
+
+def primes(i=2):
+ while True:
+ if is_prime(i):
+ yield i
+ i += 1
+
+
+def alphabet(code='', letters=''):
+ alph = ''
+ if code == 'lat' and letters == '':
+ alph = 'abcdefghijklmnopqrstuvwxyz'
+ elif code == 'bg' and letters == '':
+ alph = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ else:
+ alph = letters
+
+ for i in range(len(alph)):
+ yield alph[i]
+
+
+def intertwined_sequences(sequences=()):
+ for i in range(len(sequences)):
+ seq = sequences[i]['sequence']
+ length = sequences[i]['length']
+ result = []
+
+ if seq == 'fibonacci':
+ result.extend(list(islice(fibonacci(), length)))
+ elif seq == 'primes':
+ result.extend(list(islice(primes(), length)))
+ elif seq == 'alphabet':
+ seq_code = sequences[i]['code']
+ result.extend(list(islice(alphabet(code=seq_code), length)))
+
+ for j in range(len(result)):
+ yield result[j]