Решение на Генератори и итератори от Михаела Сейменска

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

Към профила на Михаела Сейменска

Резултати

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

Код

def fibonacci():
elem1 = 1
elem2 = 1
while True:
yield elem1
elem1, elem2 = elem2, elem1 + elem2
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(number):
for div in range(2, number):
if number % div == 0:
return False
return True
def alphabet(*, code='', letters=''):
if letters != '':
letters_list = list(letters)
letters_list.reverse()
while letters:
yield letters_list.pop()
elif code == 'lat':
letters_list = list(map(chr, range(ord('a'), ord('z') + 1)))
letters_list.reverse()
while letters_list:
yield letters_list.pop()
elif code == 'bg':
letters_list = list(map(chr, range(1072, 1104)))
letters_list.remove(chr(1101))
letters_list.remove(chr(1099))
letters_list.reverse()
while letters_list:
yield letters_list.pop()
def intertwined_sequences(input_, *, generator_definitions={}):
list_of_gens = []
gen_iters = {}
for gen in input_:
sequence = gen['sequence']
length = gen['length']
if list_of_gens.count(sequence) == 0 and sequence == 'fibonacci':
list_of_gens.append(sequence)
fibonacci_iter = fibonacci()
for _ in range(length):
yield next(fibonacci_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'fibonacci':
for _ in range(length):
yield next(fibonacci_iter)
elif list_of_gens.count(sequence) == 0 and sequence == 'primes':
list_of_gens.append(sequence)
primes_iter = primes()
for _ in range(length):
yield next(primes_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'primes':
for _ in range(length):
yield next(primes_iter)
elif list_of_gens.count(sequence) == 0 and sequence == 'alphabet':
list_of_gens.append(sequence)
if 'latters' in gen:
alphabet_iter = alphabet(letters=gen['latters'])
else:
alphabet_iter = alphabet(code=gen['code'])
for _ in range(length):
yield next(alphabet_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'alphabet':
for _ in range(length):
yield next(alphabet_iter)
elif list_of_gens.count(sequence) == 0:
gen.pop('sequence')
gen.pop('length')
list_of_gens.append(sequence)
gen_iters[sequence] = iter(generator_definitions[sequence](**gen))
for _ in range(length):
yield next(gen_iters[sequence])
else:
for _ in range(length):
yield next(gen_iters[sequence])

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

.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

----------------------------------------------------------------------
Ran 14 tests in 3.510s

FAILED (errors=1)

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

Михаела обнови решението на 01.04.2015 19:30 (преди над 9 години)

+def fibonacci():
+ elem1 = 1
+ elem2 = 1
+ while True:
+ yield elem1
+ elem1, elem2 = elem2, elem1 + elem2
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def is_prime(number):
+ for div in range(2, number):
+ if number % div == 0:
+ return False
+ return True
+
+
+def alphabet(code='', letters=''):
+ if letters != '':
+ letters_list = list(letters)
+ letters_list.reverse()
+ while letters:
+ yield letters_list.pop()
+ elif code == 'lat':
+ letters_list = list(map(chr, range(ord('a'), ord('z') + 1)))
+ letters_list.reverse()
+ while letters_list:
+ yield letters_list.pop()
+ elif code == 'bg':
+ letters_list = list(map(chr, range(1072, 1104)))
+ letters_list.remove(chr(1101))
+ letters_list.remove(chr(1099))
+ letters_list.reverse()
+ while letters_list:
+ yield letters_list.pop()
+
+
+def intertwined_sequences(input_, generator_definitions={}):
+ list_of_gens = []
+ gen_iters = {}
+ for gen in input_:
+ sequence = gen['sequence']
+ length = gen['length']
+ if list_of_gens.count(sequence) == 0 and sequence == 'fibonacci':
+ list_of_gens.append(sequence)
+ fibonacci_iter = fibonacci()
+ for _ in range(length):
+ yield next(fibonacci_iter)
+ elif list_of_gens.count(sequence) == 1 and sequence == 'fibonacci':
+ for _ in range(length):
+ yield next(fibonacci_iter)
+ elif list_of_gens.count(sequence) == 0 and sequence == 'primes':
+ list_of_gens.append(sequence)
+ primes_iter = primes()
+ for _ in range(length):
+ yield next(primes_iter)
+ elif list_of_gens.count(sequence) == 1 and sequence == 'primes':
+ for _ in range(length):
+ yield next(primes_iter)
+ elif list_of_gens.count(sequence) == 0 and sequence == 'alphabet':
+ list_of_gens.append(sequence)
+ if 'latters' in gen:
+ alphabet_iter = alphabet(letters=gen['latters'])
+ else:
+ alphabet_iter = alphabet(code=gen['code'])
+ for _ in range(length):
+ yield next(alphabet_iter)
+ elif list_of_gens.count(sequence) == 1 and sequence == 'alphabet':
+ for _ in range(length):
+ yield next(alphabet_iter)
+ elif list_of_gens.count(sequence) == 0:
+ gen.pop('sequence')
+ gen.pop('length')
+ list_of_gens.append(sequence)
+ gen_iters[sequence] = iter(generator_definitions[sequence](**gen))
+ for _ in range(length):
+ yield next(gen_iters[sequence])
+ else:
+ for _ in range(length):
+ yield next(gen_iters[sequence])

Михаела обнови решението на 03.04.2015 14:55 (преди над 9 години)

def fibonacci():
elem1 = 1
elem2 = 1
while True:
yield elem1
elem1, elem2 = elem2, elem1 + elem2
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(number):
for div in range(2, number):
if number % div == 0:
return False
return True
-def alphabet(code='', letters=''):
+def alphabet(*, code='', letters=''):
if letters != '':
letters_list = list(letters)
letters_list.reverse()
while letters:
yield letters_list.pop()
elif code == 'lat':
letters_list = list(map(chr, range(ord('a'), ord('z') + 1)))
letters_list.reverse()
while letters_list:
yield letters_list.pop()
elif code == 'bg':
letters_list = list(map(chr, range(1072, 1104)))
letters_list.remove(chr(1101))
letters_list.remove(chr(1099))
letters_list.reverse()
while letters_list:
yield letters_list.pop()
-def intertwined_sequences(input_, generator_definitions={}):
+def intertwined_sequences(input_, *, generator_definitions={}):
list_of_gens = []
gen_iters = {}
for gen in input_:
sequence = gen['sequence']
length = gen['length']
if list_of_gens.count(sequence) == 0 and sequence == 'fibonacci':
list_of_gens.append(sequence)
fibonacci_iter = fibonacci()
for _ in range(length):
yield next(fibonacci_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'fibonacci':
for _ in range(length):
yield next(fibonacci_iter)
elif list_of_gens.count(sequence) == 0 and sequence == 'primes':
list_of_gens.append(sequence)
primes_iter = primes()
for _ in range(length):
yield next(primes_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'primes':
for _ in range(length):
yield next(primes_iter)
elif list_of_gens.count(sequence) == 0 and sequence == 'alphabet':
list_of_gens.append(sequence)
if 'latters' in gen:
alphabet_iter = alphabet(letters=gen['latters'])
else:
alphabet_iter = alphabet(code=gen['code'])
for _ in range(length):
yield next(alphabet_iter)
elif list_of_gens.count(sequence) == 1 and sequence == 'alphabet':
for _ in range(length):
yield next(alphabet_iter)
elif list_of_gens.count(sequence) == 0:
gen.pop('sequence')
gen.pop('length')
list_of_gens.append(sequence)
gen_iters[sequence] = iter(generator_definitions[sequence](**gen))
for _ in range(length):
yield next(gen_iters[sequence])
else:
for _ in range(length):
yield next(gen_iters[sequence])