Решение на Генератори и итератори от Иван Георгиев

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

Към профила на Иван Георгиев

Резултати

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

Код

from itertools import cycle
def fibonacci():
start = [0, 1]
while True:
yield start[1]
start[1] += start[0]
start[0] = start[1] - start[0]
def primes():
number = 2
while True:
if len(list(filter(lambda x: number % x == 0, range(1, number)))) == 1:
yield number
number += 1
else:
number += 1
def alphabet(*, code=None, letters=None):
if letters:
for letter in letters:
yield letter
else:
alphabets = {'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
for letter in alphabets[code]:
yield letter
def intertwined_sequences(iterable, *, generator_definitions=None):
catalog = {'fibonacci': fibonacci(),
'primes': primes()}
for item in iterable:
if item['sequence'] == 'alphabet' and 'alphabet' not in catalog.keys():
catalog['alphabet'] = alphabet(code=item.get('code'),
letters=item.get('letters'))
if generator_definitions:
if item['sequence'] in generator_definitions.keys() and \
item['sequence'] not in catalog.keys():
args = {k: v for (k, v) in item.items()
if k not in ['sequence', 'length']}
catalog[item['sequence']] = \
iter(generator_definitions[item['sequence']](**args))
for i in range(item['length']):
yield next(catalog[item['sequence']])

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

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

----------------------------------------------------------------------
Ran 14 tests in 4.199s

FAILED (errors=2)

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

Иван обнови решението на 29.03.2015 12:28 (преди над 9 години)

+from itertools import cycle
+
+
+def fibonacci():
+ start = [0, 1]
+ while True:
+ yield start[1]
+ start[1] += start[0]
+ start[0] = start[1] - start[0]
+
+
+def primes():
+ number = 2
+ while True:
+ if len(list(filter(lambda x: number % x == 0, range(1, number)))) == 1:
+ yield number
+ number += 1
+ else:
+ number += 1
+
+
+def alphabet(*, code=None, letters=None):
+ if letters:
+ for letter in letters:
+ yield letter
+ else:
+ alphabets = {'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
+ for letter in alphabets[code]:
+ yield letter
+
+
+def intertwined_sequences(iterable, *, generator_definitions=None):
+ catalog = {'fibonacci': fibonacci(),
+ 'primes': primes()}
+ for item in iterable:
+ if item['sequence'] == 'alphabet' and 'alphabet' not in catalog.keys():
+ catalog['alphabet'] = alphabet(code=item.get('code'),
+ letters=item.get('letters'))
+ if generator_definitions:
+ if item['sequence'] in generator_definitions.keys() and \
+ item['sequence'] not in catalog.keys():
+ args = {k: v for (k, v) in item.items()
+ if k not in ['sequence', 'length']}
+ catalog[item['sequence']] = \
+ generator_definitions[item['sequence']](**args)
+ for i in range(item['length']):
+ yield next(catalog[item['sequence']])

Иван обнови решението на 31.03.2015 13:47 (преди над 9 години)

from itertools import cycle
def fibonacci():
start = [0, 1]
while True:
yield start[1]
start[1] += start[0]
start[0] = start[1] - start[0]
def primes():
number = 2
while True:
if len(list(filter(lambda x: number % x == 0, range(1, number)))) == 1:
yield number
number += 1
else:
number += 1
def alphabet(*, code=None, letters=None):
if letters:
for letter in letters:
yield letter
else:
alphabets = {'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
for letter in alphabets[code]:
yield letter
def intertwined_sequences(iterable, *, generator_definitions=None):
catalog = {'fibonacci': fibonacci(),
'primes': primes()}
for item in iterable:
if item['sequence'] == 'alphabet' and 'alphabet' not in catalog.keys():
catalog['alphabet'] = alphabet(code=item.get('code'),
letters=item.get('letters'))
if generator_definitions:
if item['sequence'] in generator_definitions.keys() and \
item['sequence'] not in catalog.keys():
args = {k: v for (k, v) in item.items()
if k not in ['sequence', 'length']}
catalog[item['sequence']] = \
- generator_definitions[item['sequence']](**args)
+ iter(generator_definitions[item['sequence']](**args))
for i in range(item['length']):
yield next(catalog[item['sequence']])