Решение на Генератори и итератори от Станислав Гатев

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

Към профила на Станислав Гатев

Резултати

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

Код

import itertools
ALPHABETS = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first+second
def is_prime(num):
return num > 1 and all(num % divisor for divisor in range(2, num))
def primes():
return (num for num in itertools.count() if is_prime(num))
def alphabet(code=None, letters=None):
return (letter for letter in (letters or ALPHABETS[code]))
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def intertwined_sequences(generator_descriptions, generator_definitions={}):
generators = dict(GENERATORS, **generator_definitions)
generators_cache = {}
for description in generator_descriptions:
sequence = description.pop('sequence')
length = description.pop('length')
if sequence not in generators_cache:
generators_cache[sequence] = iter(
generators[sequence](**description))
for _ in range(length):
yield next(generators_cache[sequence])

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

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

----------------------------------------------------------------------
Ran 14 tests in 3.685s

FAILED (errors=1)

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

Станислав обнови решението на 31.03.2015 21:34 (преди над 9 години)

+import itertools
+
+
+ALPHABETS = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщюя'
+}
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first+second
+
+
+def is_prime(num):
+ return num > 1 and all(num % divisor for divisor in range(2, num))
+
+
+def primes():
+ return (num for num in itertools.count() if is_prime(num))
+
+
+def alphabet(code=None, letters=None):
+ return (letter for letter in (letters or ALPHABETS[code]))
+
+
+GENERATORS = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+}
+
+
+def intertwined_sequences(generator_descriptions, generator_definitions={}):
+ generators = dict(GENERATORS, **generator_definitions)
+ generators_cache = {}
+ for description in generator_descriptions:
+ sequence = description.pop('sequence')
+ length = description.pop('length')
+ if sequence not in generators_cache:
+ generators_cache[sequence] = iter(
+ generators[sequence](**description))
+
+ for _ in range(length):
+ yield next(generators_cache[sequence])

Станислав обнови решението на 03.04.2015 10:35 (преди над 9 години)

import itertools
ALPHABETS = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
- 'bg': 'абвгдежзийклмнопрстуфхцчшщюя'
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first+second
def is_prime(num):
return num > 1 and all(num % divisor for divisor in range(2, num))
def primes():
return (num for num in itertools.count() if is_prime(num))
def alphabet(code=None, letters=None):
return (letter for letter in (letters or ALPHABETS[code]))
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def intertwined_sequences(generator_descriptions, generator_definitions={}):
generators = dict(GENERATORS, **generator_definitions)
generators_cache = {}
for description in generator_descriptions:
sequence = description.pop('sequence')
length = description.pop('length')
if sequence not in generators_cache:
generators_cache[sequence] = iter(
generators[sequence](**description))
for _ in range(length):
yield next(generators_cache[sequence])