Решение на Генератори и итератори от Кристиян Бъновски

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

Към профила на Кристиян Бъновски

Резултати

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

Код

def fibonacci():
current_num = 1
next_num = 1
while True:
yield current_num
current_num, next_num = next_num, current_num + next_num
def primes():
saved_primes = {}
prime_number = 2
while True:
if prime_number not in saved_primes:
yield prime_number
saved_primes[prime_number * prime_number] = [prime_number]
else:
for saved_prime_number in saved_primes[prime_number]:
saved_primes.setdefault(saved_prime_number + prime_number, []).append(saved_prime_number)
del saved_primes[prime_number]
prime_number += 1
def alphabet(code='', letters=''):
if letters is '':
if code is 'lat':
selected_alphabet = 'abcdefghijklmnopqrstuvwxyz'
elif code is 'bg':
selected_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
else:
selected_alphabet = letters
for position in range(len(selected_alphabet)):
yield selected_alphabet[position]
def intertwined_sequences(sequences, generator_definitions=''):
seq_inst = {}
for sequence in sequences:
seq_arg = dict(sequence)
seq_arg.pop('sequence')
seq_arg.pop('length')
if sequence['sequence'] not in seq_inst:
if sequence['sequence'] in globals():
seq_inst[sequence['sequence']] = iter(globals()[sequence['sequence']](**seq_arg))
else:
seq_inst[sequence['sequence']] = iter(generator_definitions[sequence['sequence']](**seq_arg))
for x in range(sequence['length']):
yield(next(seq_inst[sequence['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 2.184s

FAILED (errors=1)

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

Кристиян обнови решението на 03.04.2015 14:23 (преди над 9 години)

+def fibonacci():
+ current_num = 1
+ next_num = 1
+ while True:
+ yield current_num
+ current_num, next_num = next_num, current_num + next_num
+
+def primes():
+ saved_primes = {}
+ prime_number = 2
+ while True:
+ if prime_number not in saved_primes:
+ yield prime_number
+ saved_primes[prime_number * prime_number] = [prime_number]
+ else:
+ for saved_prime_number in saved_primes[prime_number]:
+ saved_primes.setdefault(saved_prime_number + prime_number, []).append(saved_prime_number)
+ del saved_primes[prime_number]
+ prime_number += 1
+
+def alphabet(code='', letters=''):
+ if letters is '':
+ if code is 'lat':
+ selected_alphabet = 'abcdefghijklmnopqrstuvwxyz'
+ elif code is 'bg':
+ selected_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ else:
+ selected_alphabet = letters
+ for position in range(len(selected_alphabet)):
+ yield selected_alphabet[position]
+
+def intertwined_sequences(sequences, generator_definitions=''):
+ seq_inst = {}
+ for sequence in sequences:
+ seq_arg = dict(sequence)
+ seq_arg.pop('sequence')
+ seq_arg.pop('length')
+ if sequence['sequence'] not in seq_inst:
+ if sequence['sequence'] in globals():
+ seq_inst[sequence['sequence']] = iter(globals()[sequence['sequence']](**seq_arg))
+ else:
+ seq_inst[sequence['sequence']] = iter(generator_definitions[sequence['sequence']](**seq_arg))
+ for x in range(sequence['length']):
+ yield(next(seq_inst[sequence['sequence']]))