Решение на Генератори и итератори от Емил Великов

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

Към профила на Емил Великов

Резултати

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

Код

from math import sqrt
BG_LETTERS = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
for i in range(2, (int(sqrt(number) + 1))):
if number % i == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet_helper(letters):
for char in letters:
yield char
def alphabet(code='', letters=''):
if not letters:
if code == 'bg':
yield from alphabet_helper(BG_LETTERS)
elif code == 'lat':
yield from alphabet_helper(LATIN_LETTERS)
else:
yield from alphabet_helper(letters)
"""Не е в началото, защото трябва да е след дефинициите на генераторите"""
PRIMARY_DEFINITIONS = {'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet}
def intertwined_sequences(iterable, generator_definitions={}):
generator_definitions = dict(generator_definitions)
generator_definitions.update(PRIMARY_DEFINITIONS)
definitions = {}
for item in iterable:
name = item['sequence']
if name not in definitions:
arguments = {k: v for k, v in item.items()
if k != 'sequence' and k != 'length'}
current_generator = iter(generator_definitions[name](**arguments))
definitions[name] = current_generator
for i in range(item['length']):
yield next(definitions[name])

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

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

FAILED (errors=1)

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

Емил обнови решението на 31.03.2015 21:43 (преди над 9 години)

+from math import sqrt
+
+
+BG_LETTERS = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+LATIN_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
+
+
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def is_prime(number):
+ for i in range(2, (int(sqrt(number) + 1))):
+ if number % i == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet_helper(letters):
+ for char in letters:
+ yield char
+
+
+def alphabet(code, letters=''):
+ if code == 'bg' and not letters:
+ yield from alphabet_helper(BG_LETTERS)
+ elif code == 'lat' and not letters:
+ yield from alphabet_helper(LATIN_LETTERS)
+ else:
+ letters = code
+ yield from alphabet_helper(letters)
+
+
+"""Не е в началото, защото трябва да е след дефинициите на генераторите"""
+PRIMARY_DEFINITIONS = {'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet}
+
+
+def intertwined_sequences(iterable, generator_definitions={}):
+ generator_definitions = dict(generator_definitions)
+ generator_definitions.update(PRIMARY_DEFINITIONS)
+ definitions = {}
+ for item in iterable:
+ name = item['sequence']
+ if name not in definitions:
+ arguments = {k: v for k, v in item.items()
+ if k != 'sequence' and k != 'length'}
+ current_generator = iter(generator_definitions[name](**arguments))
+ definitions[name] = current_generator
+ for i in range(item['length']):
+ yield next(definitions[name])

Емил обнови решението на 01.04.2015 18:45 (преди над 9 години)

from math import sqrt
BG_LETTERS = 'абвгдежзийклмнопрстуфхцчшщъьюя'
LATIN_LETTERS = 'abcdefghijklmnopqrstuvwxyz'
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def is_prime(number):
for i in range(2, (int(sqrt(number) + 1))):
if number % i == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet_helper(letters):
for char in letters:
yield char
-def alphabet(code, letters=''):
- if code == 'bg' and not letters:
- yield from alphabet_helper(BG_LETTERS)
- elif code == 'lat' and not letters:
- yield from alphabet_helper(LATIN_LETTERS)
+def alphabet(code='', letters=''):
+ if not letters:
+ if code == 'bg':
+ yield from alphabet_helper(BG_LETTERS)
+ elif code == 'lat':
+ yield from alphabet_helper(LATIN_LETTERS)
else:
- letters = code
yield from alphabet_helper(letters)
"""Не е в началото, защото трябва да е след дефинициите на генераторите"""
PRIMARY_DEFINITIONS = {'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet}
def intertwined_sequences(iterable, generator_definitions={}):
generator_definitions = dict(generator_definitions)
generator_definitions.update(PRIMARY_DEFINITIONS)
definitions = {}
for item in iterable:
name = item['sequence']
if name not in definitions:
arguments = {k: v for k, v in item.items()
if k != 'sequence' and k != 'length'}
current_generator = iter(generator_definitions[name](**arguments))
definitions[name] = current_generator
for i in range(item['length']):
yield next(definitions[name])