Решение на Генератори и итератори от Минх Нгуен

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

Към профила на Минх Нгуен

Резултати

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

Код

lat = "abcdefghijklmnopqrstuvwxyz"
bg = "абвгдежзийклмнопрстуфхцчшщъьюя"
def fibonacci():
previous_number = 0
number = 1
yield 1
while True:
yield number + previous_number
number += previous_number
previous_number = number - previous_number
def prime():
number = 2
while True:
if __is_prime(number):
yield number
number += 1
def alphabet(*, code=None, letters=None):
lang = None
if code == 'lat':
lang = lat
elif code == 'bg':
lang = bg
else:
lang = letters
letter_index = 0
while letter_index < len(lang):
yield lang[letter_index]
letter_index += 1
def __is_prime(number):
if number < 2:
return False
if number == 2:
return True
if number % 2 == 0:
return False
for current_number in range(3, number):
if number % current_number == 0:
return False
return True
def intertwined_sequences(specifications, generator_definitions=None):
generators = {
'alphabet': alphabet,
'primes': prime,
'fibonacci': fibonacci
}
generator_instances = dict()
generator_arguments = dict()
if generator_definitions is not None:
for key, value in generator_definitions.items():
generators[key] = value
for spec in specifications:
sequence = spec['sequence']
if sequence not in generator_instances:
if generator_definitions is not None and sequence in generator_definitions and len(spec) > 2:
if sequence not in generator_arguments:
argument = spec.copy()
del argument['sequence']
del argument['length']
generator_arguments[sequence] = argument
generator_instances[sequence] = iter(generators[sequence](**generator_arguments[sequence]))
elif sequence != 'alphabet':
generator_instances[sequence] = iter(generators[sequence]())
else:
if 'alphabet' not in generator_arguments:
if 'letters' in spec:
generator_arguments['alphabet'] = spec['letters']
generator_instances['alphabet'] = iter(generators['alphabet'](letters=spec['letters']))
elif 'bg' in spec.values():
generator_arguments['alphabet'] = bg
generator_instances['alphabet'] = iter(generators['alphabet'](letters=bg))
else:
generator_arguments['alphabet'] = lat
generator_instances['alphabet'] = iter(generators['alphabet'](letters=lat))
if sequence is not 'alphabet':
if len(spec) > 2:
while spec['length']:
spec['length'] -= 1
generator_instances_obj = generator_instances[sequence]
yield next(generator_instances_obj)
else:
while spec['length']:
spec['length'] -= 1
generator_instances_obj = generator_instances[sequence]
yield next(generator_instances_obj)
else:
while spec['length']:
spec['length'] -= 1
generator_instances_obj = generator_instances['alphabet']
yield next(generator_instances_obj)

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

.E.....E....EE
======================================================================
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_infinite_intertwined (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

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

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

----------------------------------------------------------------------
Ran 14 tests in 8.150s

FAILED (errors=4)

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

Минх обнови решението на 02.04.2015 23:03 (преди над 9 години)

+lat = "abcdefghijklmnopqrstuvwxyz"
+bg = "абвгдежзийклмнопрстуфхцчшщъьюя"
+
+
+def fibonacci():
+ previous_number = 0
+ number = 1
+ yield 1
+ while True:
+ yield number + previous_number
+ number += previous_number
+ previous_number = number - previous_number
+
+
+def prime():
+ number = 2
+ while True:
+ if __is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(*, code=None, letters=None):
+ lang = None
+ if code == 'lat':
+ lang = lat
+ elif code == 'bg':
+ lang = bg
+ else:
+ lang = letters
+
+ letter_index = 0
+ while letter_index < len(lang):
+ yield lang[letter_index]
+ letter_index += 1
+
+
+def __is_prime(number):
+ if number < 2:
+ return False
+ if number == 2:
+ return True
+ if number % 2 == 0:
+ return False
+ for current_number in range(3, number):
+ if number % current_number == 0:
+ return False
+ return True
+
+
+def intertwined_sequences(specifications, generator_definitions=None):
+ generators = {
+ 'alphabet': alphabet,
+ 'primes': prime,
+ 'fibonacci': fibonacci
+ }
+ generator_instances = dict()
+ generator_arguments = dict()
+
+ if generator_definitions is not None:
+ for key, value in generator_definitions.items():
+ generators[key] = value
+
+ for spec in specifications:
+ sequence = spec['sequence']
+ if sequence not in generator_instances:
+ if generator_definitions is not None and sequence in generator_definitions and len(spec) > 2:
+ if sequence not in generator_arguments:
+ argument = spec.copy()
+ del argument['sequence']
+ del argument['length']
+ generator_arguments[sequence] = argument
+ generator_instances[sequence] = iter(generators[sequence](**generator_arguments[sequence]))
+ elif sequence != 'alphabet':
+ generator_instances[sequence] = iter(generators[sequence]())
+ else:
+ if 'alphabet' not in generator_arguments:
+ if 'letters' in spec:
+ generator_arguments['alphabet'] = spec['letters']
+ generator_instances['alphabet'] = iter(generators['alphabet'](letters=spec['letters']))
+ elif 'bg' in spec.values():
+ generator_arguments['alphabet'] = bg
+ generator_instances['alphabet'] = iter(generators['alphabet'](letters=bg))
+ else:
+ generator_arguments['alphabet'] = lat
+ generator_instances['alphabet'] = iter(generators['alphabet'](letters=lat))
+
+ if sequence is not 'alphabet':
+ if len(spec) > 2:
+ while spec['length']:
+ spec['length'] -= 1
+ generator_instances_obj = generator_instances[sequence]
+ yield next(generator_instances_obj)
+ else:
+ while spec['length']:
+ spec['length'] -= 1
+ generator_instances_obj = generator_instances[sequence]
+ yield next(generator_instances_obj)
+ else:
+ while spec['length']:
+ spec['length'] -= 1
+ generator_instances_obj = generator_instances['alphabet']
+ yield next(generator_instances_obj)