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

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

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

Резултати

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

Код

def fibonacci():
last_number = 0
number = 1
while True:
yield number
last_number, number = number, last_number + number
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif kwargs['code'] == 'lat':
for latin_letter in range(97, 123):
yield chr(latin_letter)
else:
for bg_letter in range(1072, 1104):
if bg_letter != 1099 and bg_letter != 1101:
yield chr(bg_letter)
def intertwined_sequences(sequences, generator_definitions={}):
if type(sequences) is list:
generator_objects = return_generators(sequences, generator_definitions)
for generator in generator_objects:
for _ in range(generator[1]):
yield (next(generator[0]))
else:
gen_objects = {}
while True:
sequence = next(sequences)
sequence_name = sequence['sequence']
new_objects = create_generators([sequence], generator_definitions)
if sequence_name not in gen_objects:
gen_objects.update({sequence_name: new_objects[sequence_name]})
for _ in range(sequence['length']):
yield (next(gen_objects[sequence_name]))
def return_generators(sequences, gen_definitions):
generators = []
gen_objs = create_generators(sequences, gen_definitions)
for element in sequences:
generators.append([gen_objs[element['sequence']], element['length']])
return generators
def create_generators(list_of_sequences, definitions):
gen_objects = {'primes': primes(), 'fibonacci': fibonacci()}
for elem in list_of_sequences:
generator = elem['sequence']
if generator not in gen_objects:
if generator == 'alphabet':
gen_objects['alphabet'] = alphabet(**elem)
else:
gen_objects[generator] = create_gen_object(elem, definitions)
return gen_objects
def create_gen_object(sequence, gen_definitions):
args = dict(sequence)
del args['sequence']
del args['length']
return gen_definitions[sequence['sequence']](**args)

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

......E.......
======================================================================
ERROR: test_generator_definitions (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 2.289s

FAILED (errors=1)

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

Стилиян обнови решението на 01.04.2015 01:00 (преди над 9 години)

+def fibonacci():
+ last_number = 0
+ number = 1
+ while True:
+ yield number
+ last_number, number = number, last_number + number
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def is_prime(n):
+ if n <= 3:
+ return n >= 2
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ for letter in kwargs['letters']:
+ yield letter
+ elif kwargs['code'] == 'lat':
+ for latin_letter in range(97, 123):
+ yield chr(latin_letter)
+ else:
+ for bg_letter in range(1072, 1103):
+ yield che(bg_letter)

Стилиян обнови решението на 01.04.2015 01:08 (преди над 9 години)

def fibonacci():
last_number = 0
number = 1
while True:
yield number
last_number, number = number, last_number + number
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif kwargs['code'] == 'lat':
for latin_letter in range(97, 123):
yield chr(latin_letter)
else:
- for bg_letter in range(1072, 1103):
- yield che(bg_letter)
+ for bg_letter in range(1072, 1104):
+ if bg_letter != 1099 and bg_letter != 1101:
+ yield che(bg_letter)

Стилиян обнови решението на 03.04.2015 01:27 (преди над 9 години)

def fibonacci():
last_number = 0
number = 1
while True:
yield number
last_number, number = number, last_number + number
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif kwargs['code'] == 'lat':
for latin_letter in range(97, 123):
yield chr(latin_letter)
else:
for bg_letter in range(1072, 1104):
if bg_letter != 1099 and bg_letter != 1101:
- yield che(bg_letter)
+ yield chr(bg_letter)
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ generator_objects = return_generators(sequences, generator_definitions)
+ for generator in generator_objects:
+ for _ in range(generator[1]):
+ yield (next(generator[0]))
+
+
+def return_generators(sequences, gen_definitions):
+ generators = []
+ gen_objs = return_generator_objects(sequences, gen_definitions)
+ for element in sequences:
+ generators.append([gen_objs[element['sequence']], element['length']])
+ return generators
+
+
+def return_generator_objects(list_of_sequences, definitions):
+ gen_objects = {'primes': primes(), 'fibonacci': fibonacci()}
+ for elem in list_of_sequences:
+ generator = elem['sequence']
+ if generator not in gen_objects:
+ if generator == 'alphabet':
+ gen_objects['alphabet'] = alphabet(**elem)
+ else:
+ gen_objects[generator] = create_gen_object(elem, definitions)
+ return gen_objects
+
+
+def create_gen_object(sequence, gen_definitions):
+ args = dict(sequence)
+ del args['sequence']
+ del args['length']
+ return gen_definitions[sequence['sequence']](**args)

Стилиян обнови решението на 03.04.2015 11:27 (преди над 9 години)

def fibonacci():
last_number = 0
number = 1
while True:
yield number
last_number, number = number, last_number + number
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif kwargs['code'] == 'lat':
for latin_letter in range(97, 123):
yield chr(latin_letter)
else:
for bg_letter in range(1072, 1104):
if bg_letter != 1099 and bg_letter != 1101:
yield chr(bg_letter)
def intertwined_sequences(sequences, generator_definitions={}):
- generator_objects = return_generators(sequences, generator_definitions)
- for generator in generator_objects:
- for _ in range(generator[1]):
- yield (next(generator[0]))
+ if type(sequences) is list:
+ generator_objects = return_generators(sequences, generator_definitions)
+ for generator in generator_objects:
+ for _ in range(generator[1]):
+ yield (next(generator[0]))
+ else:
+ gen_objects = {}
+ while True:
+ current_sequence = next(sequences)
+ sequence_name = current_sequence['sequence']
+ new_objects = return_generator_objects([current_sequence], {})
+ if sequence_name not in gen_objects:
+ gen_objects.update({sequence_name: new_objects[sequence_name]})
+ for _ in range(current_sequence['length']):
+ yield (next(gen_objects[sequence_name]))
def return_generators(sequences, gen_definitions):
generators = []
gen_objs = return_generator_objects(sequences, gen_definitions)
for element in sequences:
generators.append([gen_objs[element['sequence']], element['length']])
return generators
def return_generator_objects(list_of_sequences, definitions):
gen_objects = {'primes': primes(), 'fibonacci': fibonacci()}
for elem in list_of_sequences:
generator = elem['sequence']
if generator not in gen_objects:
if generator == 'alphabet':
gen_objects['alphabet'] = alphabet(**elem)
else:
gen_objects[generator] = create_gen_object(elem, definitions)
return gen_objects
def create_gen_object(sequence, gen_definitions):
args = dict(sequence)
del args['sequence']
del args['length']
return gen_definitions[sequence['sequence']](**args)

Стилиян обнови решението на 03.04.2015 11:39 (преди над 9 години)

def fibonacci():
last_number = 0
number = 1
while True:
yield number
last_number, number = number, last_number + number
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
yield letter
elif kwargs['code'] == 'lat':
for latin_letter in range(97, 123):
yield chr(latin_letter)
else:
for bg_letter in range(1072, 1104):
if bg_letter != 1099 and bg_letter != 1101:
yield chr(bg_letter)
def intertwined_sequences(sequences, generator_definitions={}):
if type(sequences) is list:
generator_objects = return_generators(sequences, generator_definitions)
for generator in generator_objects:
for _ in range(generator[1]):
yield (next(generator[0]))
else:
gen_objects = {}
while True:
- current_sequence = next(sequences)
- sequence_name = current_sequence['sequence']
- new_objects = return_generator_objects([current_sequence], {})
+ sequence = next(sequences)
+ sequence_name = sequence['sequence']
+ new_objects = create_generators([sequence], generator_definitions)
if sequence_name not in gen_objects:
gen_objects.update({sequence_name: new_objects[sequence_name]})
- for _ in range(current_sequence['length']):
+ for _ in range(sequence['length']):
yield (next(gen_objects[sequence_name]))
def return_generators(sequences, gen_definitions):
generators = []
- gen_objs = return_generator_objects(sequences, gen_definitions)
+ gen_objs = create_generators(sequences, gen_definitions)
for element in sequences:
generators.append([gen_objs[element['sequence']], element['length']])
return generators
-def return_generator_objects(list_of_sequences, definitions):
+def create_generators(list_of_sequences, definitions):
gen_objects = {'primes': primes(), 'fibonacci': fibonacci()}
for elem in list_of_sequences:
generator = elem['sequence']
if generator not in gen_objects:
if generator == 'alphabet':
gen_objects['alphabet'] = alphabet(**elem)
else:
gen_objects[generator] = create_gen_object(elem, definitions)
return gen_objects
def create_gen_object(sequence, gen_definitions):
args = dict(sequence)
del args['sequence']
del args['length']
return gen_definitions[sequence['sequence']](**args)