Решение на Генератори и итератори от Йордан Русев

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

Към профила на Йордан Русев

Резултати

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

Код

def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
a = 2
result = set()
while True:
for i in result:
if a % i == 0:
break
else:
result.add(a)
yield a
a += 1
def alphabet(code=None, letters=None):
if letters is not None:
x = 0
while True:
if x >= len(letters):
break
else:
yield letters[x]
x += 1
elif code == 'lat':
i = 97
while True:
if i > 122:
break
else:
yield chr(i)
i += 1
elif code == 'bg':
j = 1072
while True:
if j > 1103:
break
elif j == 1099 or j == 1101:
j += 1
else:
yield chr(j)
j += 1
def intertwined_sequences(iterable, generator_definitions=None):
global_dict = {}
fib = fibonacci()
prim = primes()
for hash_table in iterable:
i = ""
length = 0
cd = ""
lt = None
k = {}
for name, value in hash_table.items():
if name == "length":
length = value
elif name == "code":
cd = value
elif name == "letters":
lt = value
elif name == "sequence":
i = value
else:
k.update({name: value})
if i not in global_dict:
if i != 'fibonacci' and i != 'primes'and i != 'alphabet':
if k:
global_dict[i] = generator_definitions[i](**k)
else:
global_dict[i] = generator_definitions[i]()
alpha = alphabet(code=cd, letters=lt)
for j in range(length):
if i == 'fibonacci':
yield next(fib)
elif i == 'primes':
yield next(prim)
elif i == 'alphabet':
yield next(alpha)
else:
yield next(global_dict[i])

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

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

FAILED (errors=4)

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

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

+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ a = 2
+ result = set()
+ while True:
+ for i in result:
+ if a % i == 0:
+ break
+ else:
+ result.add(a)
+ yield a
+ a += 1
+
+
+def alphabet(code=None, letters=None):
+ if letters is not None:
+ x = 0
+ while True:
+ if x >= len(letters):
+ break
+ else:
+ yield letters[x]
+ x += 1
+ elif code == 'lat':
+ i = 97
+ while True:
+ if i > 122:
+ break
+ else:
+ yield chr(i)
+ i += 1
+ elif code == 'bg':
+ j = 1072
+ while True:
+ if j > 1103:
+ break
+ elif j == 1099 or j == 1101:
+ j += 1
+ else:
+ yield chr(j)
+ j += 1
+
+
+def intertwined_sequences(iterable, generator_definitions=None):
+ global_dict = {}
+ fib = fibonacci()
+ prim = primes()
+ for hash_table in iterable:
+ i = ""
+ length = 0
+ cd = ""
+ lt = None
+ k = {}
+ for name, value in hash_table.items():
+ if name == "length":
+ length = value
+ elif name == "code":
+ cd = value
+ elif name == "letters":
+ lt = value
+ elif name == "sequence":
+ i = value
+ else:
+ k.update({name: value})
+ if i not in global_dict:
+ if i != 'fibonacci' and i != 'primes'and i != 'alphabet':
+ if k:
+ global_dict[i] = generator_definitions[i](**k)
+ else:
+ global_dict[i] = generator_definitions[i]()
+ alpha = alphabet(code=cd, letters=lt)
+ for j in range(length):
+ if i == 'fibonacci':
+ yield next(fib)
+ elif i == 'primes':
+ yield next(prim)
+ elif i == 'alphabet':
+ yield next(alpha)
+ else:
+ yield next(global_dict[i])