Решение на Генератори и итератори от Виктор Цонев

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

Към профила на Виктор Цонев

Резултати

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

Код

def fibonacci():
a, b = 1, 1
while 1:
yield a
a, b = b, a+b
from math import sqrt
def primes():
def is_simple(s):
for i in range(2,int(sqrt(s)) + 1):
if s % i == 0:
return False
return True
n = 2
while 1:
if is_simple(n):
yield n
n += 1
def alphabet(**kwargs):
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
lat = 'abcdefghijklmnopqrstuvwxyz'
if 'letters' in kwargs:
for i in kwargs['letters']:
yield i
elif kwargs['code'] == 'bg':
for i in bg:
yield i
elif kwargs['code'] == 'lat':
for i in lat:
yield i
def intertwined_sequences(s, generator_definitions={}):
fib = fibonacci()
pri = primes()
for i in s:
if i['sequence'] == 'alphabet':
if 'letters' in i:
alph = alphabet(letters=i['letters'])
if 'code' in i:
alph = alphabet(code=i['code'])
break
for i in s:
for k in range(i['length']):
if i['sequence'] == 'fibonacci':
for l in range(i['length']):
yield next(fib)
if i['sequence'] == 'primes':
for l in range(i['length']):
yield next(pri)
if i['sequence'] == 'alphabet':
for l in range(i['length']):
yield next(alph)
if generator_definitions:
for key, value in generator_definitions.items():
if i['sequence'] == key:
sequence = value()
for l in range(i['length']):
yield next(sequence)

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

......EEEEEE..
======================================================================
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

----------------------------------------------------------------------
Ran 14 tests in 12.241s

FAILED (errors=6)

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

Виктор обнови решението на 03.04.2015 16:59 (преди над 9 години)

+def fibonacci():
+ a, b = 1, 1
+ while 1:
+ yield a
+ a, b = b, a+b
+
+
+from math import sqrt
+
+
+def primes():
+ def is_simple(s):
+ for i in range(2,int(sqrt(s)) + 1):
+ if s % i == 0:
+ return False
+ return True
+ n = 2
+ while 1:
+ if is_simple(n):
+ yield n
+ n += 1
+
+
+def alphabet(**kwargs):
+ bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ lat = 'abcdefghijklmnopqrstuvwxyz'
+ if 'letters' in kwargs:
+ for i in kwargs['letters']:
+ yield i
+ elif kwargs['code'] == 'bg':
+ for i in bg:
+ yield i
+ elif kwargs['code'] == 'lat':
+ for i in lat:
+ yield i
+
+
+def intertwined_sequences(s, generator_definitions={}):
+ fib = fibonacci()
+ pri = primes()
+ for i in s:
+ if i['sequence'] == 'alphabet':
+ if 'letters' in i:
+ alph = alphabet(letters=i['letters'])
+ if 'code' in i:
+ alph = alphabet(code=i['code'])
+ break
+ for i in s:
+ for k in range(i['length']):
+ if i['sequence'] == 'fibonacci':
+ for l in range(i['length']):
+ yield next(fib)
+ if i['sequence'] == 'primes':
+ for l in range(i['length']):
+ yield next(pri)
+ if i['sequence'] == 'alphabet':
+ for l in range(i['length']):
+ yield next(alph)
+ if generator_definitions:
+ for key, value in generator_definitions.items():
+ if i['sequence'] == key:
+ sequence = value()
+ for l in range(i['length']):
+ yield next(sequence)

Виктор обнови решението на 03.04.2015 16:59 (преди над 9 години)

def fibonacci():
a, b = 1, 1
while 1:
yield a
a, b = b, a+b
-from math import sqrt
+from math import sqrt
def primes():
def is_simple(s):
for i in range(2,int(sqrt(s)) + 1):
if s % i == 0:
return False
return True
n = 2
while 1:
if is_simple(n):
yield n
n += 1
def alphabet(**kwargs):
bg = 'абвгдежзийклмнопрстуфхцчшщъьюя'
lat = 'abcdefghijklmnopqrstuvwxyz'
if 'letters' in kwargs:
for i in kwargs['letters']:
yield i
elif kwargs['code'] == 'bg':
for i in bg:
yield i
elif kwargs['code'] == 'lat':
for i in lat:
yield i
def intertwined_sequences(s, generator_definitions={}):
fib = fibonacci()
pri = primes()
for i in s:
if i['sequence'] == 'alphabet':
if 'letters' in i:
alph = alphabet(letters=i['letters'])
if 'code' in i:
alph = alphabet(code=i['code'])
break
for i in s:
for k in range(i['length']):
if i['sequence'] == 'fibonacci':
for l in range(i['length']):
yield next(fib)
if i['sequence'] == 'primes':
for l in range(i['length']):
yield next(pri)
if i['sequence'] == 'alphabet':
for l in range(i['length']):
yield next(alph)
if generator_definitions:
for key, value in generator_definitions.items():
if i['sequence'] == key:
sequence = value()
for l in range(i['length']):
yield next(sequence)