Решение на Генератори и итератори от Цветелина Александрова

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

Към профила на Цветелина Александрова

Резултати

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

Код

def fibonacci():
first_num, second_num = 1, 1
while 1:
yield first_num
first_num, second_num = second_num, first_num + second_num
def primes():
current = 2
while 1:
yield current
current += 1
while not is_prime(current):
current += 1
def is_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
def alphabet(**kwargs):
predefined_letters = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'
}
if kwargs is not None:
for key, value in kwargs.items():
if key == 'letters':
return gen_alphabet(value)
if key == 'code':
return gen_alphabet(predefined_letters[value])
def gen_alphabet(letters):
current = 0
while 1:
try:
yield letters[current]
current += 1
except IndexError:
raise StopIteration
iter_definitions = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
def intertwined_sequences(iterable_object, **kwargs):
used_iterables = []
if kwargs is not None:
if 'generator_definitions' in kwargs:
iter_definitions.update(kwargs['generator_definitions'])
for iterable in iterable_object:
if not any(item['sequence'] == iterable['sequence']
for item in used_iterables):
iterable_params = {key: value for key, value in iterable.items()
if not key == 'sequence' and
not key == 'length'}
if iterable_params:
current = iter(iter_definitions[
iterable['sequence']](**iterable_params))
else:
current = iter(iter_definitions[iterable['sequence']]())
used_iterables.append({
'sequence': iterable['sequence'],
'iter': current
})
for used_iterable in used_iterables:
if iterable['sequence'] == used_iterable['sequence']:
if 'length' in iterable:
for i in range(0, iterable['length']):
yield next(used_iterable['iter'])
else:
yield next(used_iterable['iter'])

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

.E.....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

======================================================================
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 5.581s

FAILED (errors=2)

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

Цветелина обнови решението на 01.04.2015 16:09 (преди над 9 години)

+def fibonacci():
+ first_num, second_num = 1, 1
+ while 1:
+ yield first_num
+ first_num, second_num = second_num, first_num + second_num
+
+
+def primes():
+ current = 2
+ while 1:
+ yield current
+ current += 1
+ while not is_prime(current):
+ current += 1
+
+
+def is_prime(number):
+ for i in range(2, number):
+ if number % i == 0:
+ return False
+ return True
+
+
+def alphabet(**kwargs):
+ predefined_letters = {
+ 'bg': 'абвгдежзийклмнопрстуфхюцшщъьюя',
+ 'lat': 'abcdefghijklmnopqrstuvwxyz'
+ }
+ if kwargs is not None:
+ for key, value in kwargs.items():
+ if key == 'letters':
+ return gen_alphabet(value)
+ if key == 'code':
+ return gen_alphabet(predefined_letters[value])
+
+
+def gen_alphabet(letters):
+ current = 0
+ while 1:
+ try:
+ yield letters[current]
+ current += 1
+ except IndexError:
+ raise StopIteration
+
+
+iter_definitions = {
+ 'fibonacci': fibonacci,
+ 'primes': primes, 'alphabet': alphabet
+}
+
+
+def intertwined_sequences(iterable_object, **kwargs):
+ used_iterables = []
+ if kwargs is not None:
+ if 'generator_definitions' in kwargs:
+ iter_definitions.update(kwargs['generator_definitions'])
+ for iterable in iterable_object:
+ if not any(item['sequence'] == iterable['sequence']
+ for item in used_iterables):
+ iterable_params = {key: value for key, value in iterable.items()
+ if not key == 'sequence' and
+ not key == 'length'}
+ if iterable_params:
+ current = iter(iter_definitions[
+ iterable['sequence']](**iterable_params))
+ else:
+ current = iter(iter_definitions[iterable['sequence']]())
+ used_iterables.append({
+ 'sequence': iterable['sequence'],
+ 'iter': current
+ })
+ for used_iterable in used_iterables:
+ if iterable['sequence'] == used_iterable['sequence']:
+ if 'length' in iterable:
+ for i in range(0, iterable['length']):
+ yield next(used_iterable['iter'])
+ else:
+ yield next(used_iterable['iter'])

Цветелина обнови решението на 02.04.2015 22:42 (преди над 9 години)

def fibonacci():
first_num, second_num = 1, 1
while 1:
yield first_num
first_num, second_num = second_num, first_num + second_num
def primes():
current = 2
while 1:
yield current
current += 1
while not is_prime(current):
current += 1
def is_prime(number):
for i in range(2, number):
if number % i == 0:
return False
return True
def alphabet(**kwargs):
predefined_letters = {
- 'bg': 'абвгдежзийклмнопрстуфхюцшщъьюя',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz'
}
if kwargs is not None:
for key, value in kwargs.items():
if key == 'letters':
return gen_alphabet(value)
if key == 'code':
return gen_alphabet(predefined_letters[value])
def gen_alphabet(letters):
current = 0
while 1:
try:
yield letters[current]
current += 1
except IndexError:
raise StopIteration
iter_definitions = {
'fibonacci': fibonacci,
'primes': primes, 'alphabet': alphabet
}
def intertwined_sequences(iterable_object, **kwargs):
used_iterables = []
if kwargs is not None:
if 'generator_definitions' in kwargs:
iter_definitions.update(kwargs['generator_definitions'])
for iterable in iterable_object:
if not any(item['sequence'] == iterable['sequence']
for item in used_iterables):
iterable_params = {key: value for key, value in iterable.items()
if not key == 'sequence' and
not key == 'length'}
if iterable_params:
current = iter(iter_definitions[
iterable['sequence']](**iterable_params))
else:
current = iter(iter_definitions[iterable['sequence']]())
used_iterables.append({
'sequence': iterable['sequence'],
'iter': current
})
for used_iterable in used_iterables:
if iterable['sequence'] == used_iterable['sequence']:
if 'length' in iterable:
for i in range(0, iterable['length']):
yield next(used_iterable['iter'])
else:
yield next(used_iterable['iter'])