Решение на Генератори и итератори от Елица Христова

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

Към профила на Елица Христова

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 12 успешни тест(а)
  • 2 неуспешни тест(а)

Код

from math import sqrt
import itertools
def fibonacci():
x = 1
y = 0
while True:
yield x + y
y = y + x
x = y - x
def primes():
return (x for x in itertools.count(2, 1) if is_prime(x))
def is_prime(n):
if n == 1:
return False
return len(list(filter(
lambda x: abs(n) % x == 0, range(2, int(sqrt(abs(n))) + 1)))) == 0
def alphabet(*, code=0, letters=0):
lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
if letters != 0:
for letter in letters:
yield letter
elif code == 'lat':
for letter in lat_alphabet:
yield letter
else:
for letter in bg_alphabet:
yield letter
def create_functions(row_description, generator_definitions):
all_functions = dict()
for function in row_description:
if function['sequence'] not in all_functions:
arguments = dict()
if function['sequence'] == 'fibonacci':
all_functions['fibonacci'] = fibonacci()
elif function['sequence'] == 'primes':
all_functions['primes'] = primes()
else:
for attribute in function:
if attribute != 'sequence' and attribute != 'length':
arguments[attribute] = function[attribute]
if function['sequence'] == 'alphabet':
all_functions[function['sequence']] = alphabet(**arguments)
else:
all_functions[function[
'sequence']] = generator_definitions[
function['sequence']](**arguments)
return all_functions
def intertwined_sequences(row_description, *, generator_definitions=0):
all_functions = create_functions(row_description, generator_definitions)
for function in row_description:
for number in range(0, function['length']):
yield next(all_functions[function['sequence']])

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

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

----------------------------------------------------------------------
Ran 14 tests in 4.559s

FAILED (errors=2)

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

Елица обнови решението на 02.04.2015 00:58 (преди над 9 години)

+from math import sqrt
+import itertools
+
+
+def fibonacci():
+ x = 1
+ y = 0
+ while True:
+ yield x + y
+ y = y + x
+ x = y - x
+
+
+def primes():
+ return (x for x in itertools.count(2, 1) if is_prime(x))
+
+
+def is_prime(n):
+ if n == 1:
+ return False
+ return len(list(filter(
+ lambda x: abs(n) % x == 0, range(2, int(sqrt(abs(n))) + 1)))) == 0
+
+
+def alphabet(code=0, letters=0):
+ lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+ 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+ 'y', 'z']
+ bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
+ 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+ if hasattr(letters, '__iter__'):
+ for x in letters:
+ yield x
+ elif code == 'lat':
+ for x in lat_alphabet:
+ yield x
+ else:
+ for x in bg_alphabet:
+ yield x
+
+
+def create_functions(row_description, generator_definitions):
+ all_functions = dict()
+ for function in row_description:
+ if function['sequence'] not in all_functions:
+ arguments = dict()
+ if function['sequence'] == 'fibonacci':
+ all_functions['fibonacci'] = fibonacci()
+ elif function['sequence'] == 'primes':
+ all_functions['primes'] = primes()
+ else:
+ for attribute in function:
+ if attribute != 'sequence' and attribute != 'length':
+ arguments[attribute] = function[attribute]
+ if function['sequence'] == 'alphabet':
+ all_functions[function['sequence']] = alphabet(**arguments)
+ else:
+ all_functions[function[
+ 'sequence']] = generator_definitions[
+ function['sequence']](**arguments)
+ return all_functions
+
+
+def intertwined_sequences(row_description, generator_definitions=0):
+ all_functions = create_functions(row_description, generator_definitions)
+ for function in row_description:
+ for number in range(0, function['length']):
+ yield next(all_functions[function['sequence']])

Елица обнови решението на 02.04.2015 00:59 (преди над 9 години)

from math import sqrt
import itertools
def fibonacci():
x = 1
y = 0
while True:
yield x + y
y = y + x
x = y - x
def primes():
return (x for x in itertools.count(2, 1) if is_prime(x))
def is_prime(n):
if n == 1:
return False
return len(list(filter(
lambda x: abs(n) % x == 0, range(2, int(sqrt(abs(n))) + 1)))) == 0
def alphabet(code=0, letters=0):
lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
- if hasattr(letters, '__iter__'):
+ if letters != 0:
for x in letters:
yield x
elif code == 'lat':
for x in lat_alphabet:
yield x
else:
for x in bg_alphabet:
yield x
def create_functions(row_description, generator_definitions):
all_functions = dict()
for function in row_description:
if function['sequence'] not in all_functions:
arguments = dict()
if function['sequence'] == 'fibonacci':
all_functions['fibonacci'] = fibonacci()
elif function['sequence'] == 'primes':
all_functions['primes'] = primes()
else:
for attribute in function:
if attribute != 'sequence' and attribute != 'length':
arguments[attribute] = function[attribute]
if function['sequence'] == 'alphabet':
all_functions[function['sequence']] = alphabet(**arguments)
else:
all_functions[function[
'sequence']] = generator_definitions[
function['sequence']](**arguments)
return all_functions
def intertwined_sequences(row_description, generator_definitions=0):
all_functions = create_functions(row_description, generator_definitions)
for function in row_description:
for number in range(0, function['length']):
yield next(all_functions[function['sequence']])

Елица обнови решението на 02.04.2015 01:03 (преди над 9 години)

from math import sqrt
import itertools
def fibonacci():
x = 1
y = 0
while True:
yield x + y
y = y + x
x = y - x
def primes():
return (x for x in itertools.count(2, 1) if is_prime(x))
def is_prime(n):
if n == 1:
return False
return len(list(filter(
lambda x: abs(n) % x == 0, range(2, int(sqrt(abs(n))) + 1)))) == 0
def alphabet(code=0, letters=0):
lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
if letters != 0:
- for x in letters:
- yield x
+ for letter in letters:
+ yield letter
elif code == 'lat':
- for x in lat_alphabet:
- yield x
+ for letter in lat_alphabet:
+ yield letter
else:
- for x in bg_alphabet:
- yield x
+ for letter in bg_alphabet:
+ yield letter
def create_functions(row_description, generator_definitions):
all_functions = dict()
for function in row_description:
if function['sequence'] not in all_functions:
arguments = dict()
if function['sequence'] == 'fibonacci':
all_functions['fibonacci'] = fibonacci()
elif function['sequence'] == 'primes':
all_functions['primes'] = primes()
else:
for attribute in function:
if attribute != 'sequence' and attribute != 'length':
arguments[attribute] = function[attribute]
if function['sequence'] == 'alphabet':
all_functions[function['sequence']] = alphabet(**arguments)
else:
all_functions[function[
'sequence']] = generator_definitions[
function['sequence']](**arguments)
return all_functions
def intertwined_sequences(row_description, generator_definitions=0):
all_functions = create_functions(row_description, generator_definitions)
for function in row_description:
for number in range(0, function['length']):
yield next(all_functions[function['sequence']])

Елица обнови решението на 03.04.2015 15:42 (преди над 9 години)

from math import sqrt
import itertools
def fibonacci():
x = 1
y = 0
while True:
yield x + y
y = y + x
x = y - x
def primes():
return (x for x in itertools.count(2, 1) if is_prime(x))
def is_prime(n):
if n == 1:
return False
return len(list(filter(
lambda x: abs(n) % x == 0, range(2, int(sqrt(abs(n))) + 1)))) == 0
-def alphabet(code=0, letters=0):
+def alphabet(*, code=0, letters=0):
lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z']
bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л',
'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
if letters != 0:
for letter in letters:
yield letter
elif code == 'lat':
for letter in lat_alphabet:
yield letter
else:
for letter in bg_alphabet:
yield letter
def create_functions(row_description, generator_definitions):
all_functions = dict()
for function in row_description:
if function['sequence'] not in all_functions:
arguments = dict()
if function['sequence'] == 'fibonacci':
all_functions['fibonacci'] = fibonacci()
elif function['sequence'] == 'primes':
all_functions['primes'] = primes()
else:
for attribute in function:
if attribute != 'sequence' and attribute != 'length':
arguments[attribute] = function[attribute]
if function['sequence'] == 'alphabet':
all_functions[function['sequence']] = alphabet(**arguments)
else:
all_functions[function[
'sequence']] = generator_definitions[
function['sequence']](**arguments)
return all_functions
-def intertwined_sequences(row_description, generator_definitions=0):
+def intertwined_sequences(row_description, *, generator_definitions=0):
all_functions = create_functions(row_description, generator_definitions)
for function in row_description:
for number in range(0, function['length']):
- yield next(all_functions[function['sequence']])
+ yield next(all_functions[function['sequence']])