Решение на Генератори и итератори от Кристиан Цветков

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

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

Резултати

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

Код

import math
from itertools import chain, islice, count
def fibonacci():
prev, current = 0, 1
while True:
yield current
prev, current = current, prev + current
def primes():
for n in count(2):
if is_prime(n):
yield n
def is_prime(number):
square_root = int(math.sqrt(number) + 1)
for divisor in range(2, square_root):
if number % divisor == 0:
return False
return True
LETTERS = {
'lat': 'abcdefzhiklmnopqrstvx',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
def alphabet(*, code=None, letters=None):
if code is not None and letters is None:
return iter(LETTERS[code])
elif letters is not None:
return iter(letters)
def intertwined_sequences(wraps):
result = iter(())
gen_cache = {
'fibonacci': None,
'primes': None,
'alphabet': {'lat': None, 'bg': None}
}
for wrap in wraps:
if wrap['sequence'] == 'alphabet':
result = chain(result, alphabet_generator(wrap, gen_cache))
else:
result = chain(result, number_generator(wrap, gen_cache))
return result
def alphabet_generator(wrap, gen_cache):
if 'letters' in wrap:
alphabet_iter = letter_generator(wrap, gen_cache)
else:
alphabet_iter = code_generator(wrap, gen_cache)
return (letter for letter in alphabet_iter)
def letter_generator(wrap, gen_cache):
delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
letters_ = wrap['letters']
if letters_ not in gen_cache['alphabet']:
gen_cache['alphabet'][letters_] = delegate(letters=letters_)
generator = gen_cache['alphabet'][letters_]
return islice(generator, length)
def code_generator(wrap, gen_cache):
delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
code_ = wrap['code']
if gen_cache['alphabet'][code_] is None:
gen_cache['alphabet'][code_] = delegate(code=code_)
generator = gen_cache['alphabet'][code_]
return islice(generator, length)
def number_generator(wrap, gen_cache):
function_delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
function_name = wrap['sequence']
if gen_cache[function_name] is None:
gen_cache[function_name] = function_delegate()
generator = gen_cache[function_name]
number_iter = islice(generator, length)
return (number for number in number_iter)

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

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 10.215s

FAILED (errors=5)

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

Кристиан обнови решението на 03.04.2015 02:02 (преди над 9 години)

+import math
+from itertools import chain, islice, count
+
+
+def fibonacci():
+ prev, current = 0, 1
+ while True:
+ yield current
+ prev, current = current, prev + current
+
+
+def primes():
+ for n in count(2):
+ if is_prime(n):
+ yield n
+
+
+def is_prime(number):
+ square_root = int(math.sqrt(number) + 1)
+ for divisor in range(2, square_root):
+ if number % divisor == 0:
+ return False
+ return True
+
+
+LETTERS = {
+ 'lat': 'abcdefzhiklmnopqrstvx',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
+
+
+def alphabet(*, code=None, letters=None):
+ if code is not None and letters is None:
+ return iter(LETTERS[code])
+ elif letters is not None:
+ return iter(letters)
+
+
+def intertwined_sequences(iterable):
+ result = iter(())
+ for wrap in iterable:
+ if wrap['sequence'] == 'alphabet':
+ result = chain(result, alphabet_generator(wrap))
+ else:
+ result = chain(result, other_generator(wrap))
+ return result
+
+
+def alphabet_generator(wrap):
+ method_name = globals()[wrap['sequence']]
+ length = int(wrap['length'])
+ if 'letters' in wrap:
+ alphabet_iter = islice(method_name(letters=wrap['letters']), length)
+ else:
+ alphabet_iter = islice(method_name(code=wrap['code']), length)
+ return (letter for letter in alphabet_iter)
+
+
+def other_generator(wrap):
+ method_name = globals()[wrap['sequence']]
+ length = int(wrap['length'])
+ other_iter = islice(method_name(), length)
+ return (number for number in other_iter)

Кристиан обнови решението на 03.04.2015 15:06 (преди над 9 години)

import math
from itertools import chain, islice, count
def fibonacci():
prev, current = 0, 1
while True:
yield current
prev, current = current, prev + current
def primes():
for n in count(2):
if is_prime(n):
yield n
def is_prime(number):
square_root = int(math.sqrt(number) + 1)
for divisor in range(2, square_root):
if number % divisor == 0:
return False
return True
LETTERS = {
'lat': 'abcdefzhiklmnopqrstvx',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
def alphabet(*, code=None, letters=None):
if code is not None and letters is None:
return iter(LETTERS[code])
elif letters is not None:
return iter(letters)
def intertwined_sequences(iterable):
result = iter(())
+ gen_cache = {
+ 'fibonacci': None,
+ 'primes': None,
+ 'alphabet': {'lat': None, 'bg': None}
+ }
for wrap in iterable:
if wrap['sequence'] == 'alphabet':
- result = chain(result, alphabet_generator(wrap))
+ result = chain(result, alphabet_generator(wrap, gen_cache))
else:
- result = chain(result, other_generator(wrap))
+ result = chain(result, number_generator(wrap, gen_cache))
return result
-def alphabet_generator(wrap):
- method_name = globals()[wrap['sequence']]
- length = int(wrap['length'])
+def alphabet_generator(wrap, gen_cache):
if 'letters' in wrap:
- alphabet_iter = islice(method_name(letters=wrap['letters']), length)
+ alphabet_iter = letter_generator(wrap, gen_cache)
else:
- alphabet_iter = islice(method_name(code=wrap['code']), length)
+ alphabet_iter = code_generator(wrap, gen_cache)
return (letter for letter in alphabet_iter)
-def other_generator(wrap):
- method_name = globals()[wrap['sequence']]
+def letter_generator(wrap, gen_cache):
+ delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
- other_iter = islice(method_name(), length)
- return (number for number in other_iter)
+ letters_ = wrap['letters']
+ if letters_ not in gen_cache['alphabet']:
+ gen_cache['alphabet'][letters_] = delegate(letters=letters_)
+ generator = gen_cache['alphabet'][letters_]
+ return islice(generator, length)
+
+
+def code_generator(wrap, gen_cache):
+ delegate = globals()[wrap['sequence']]
+ length = int(wrap['length'])
+ code_ = wrap['code']
+ if gen_cache['alphabet'][code_] is None:
+ gen_cache['alphabet'][code_] = delegate(code=code_)
+ generator = gen_cache['alphabet'][code_]
+ return islice(generator, length)
+
+
+def number_generator(wrap, gen_cache):
+ function_delegate = globals()[wrap['sequence']]
+ length = int(wrap['length'])
+ function_name = wrap['sequence']
+ if gen_cache[function_name] is None:
+ gen_cache[function_name] = function_delegate()
+ generator = gen_cache[function_name]
+ number_iter = islice(generator, length)
+ return (number for number in number_iter)

Кристиан обнови решението на 03.04.2015 15:08 (преди над 9 години)

import math
from itertools import chain, islice, count
def fibonacci():
prev, current = 0, 1
while True:
yield current
prev, current = current, prev + current
def primes():
for n in count(2):
if is_prime(n):
yield n
def is_prime(number):
square_root = int(math.sqrt(number) + 1)
for divisor in range(2, square_root):
if number % divisor == 0:
return False
return True
LETTERS = {
'lat': 'abcdefzhiklmnopqrstvx',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
def alphabet(*, code=None, letters=None):
if code is not None and letters is None:
return iter(LETTERS[code])
elif letters is not None:
return iter(letters)
-def intertwined_sequences(iterable):
+def intertwined_sequences(wraps):
result = iter(())
gen_cache = {
'fibonacci': None,
'primes': None,
'alphabet': {'lat': None, 'bg': None}
}
- for wrap in iterable:
+ for wrap in wraps:
if wrap['sequence'] == 'alphabet':
result = chain(result, alphabet_generator(wrap, gen_cache))
else:
result = chain(result, number_generator(wrap, gen_cache))
return result
def alphabet_generator(wrap, gen_cache):
if 'letters' in wrap:
alphabet_iter = letter_generator(wrap, gen_cache)
else:
alphabet_iter = code_generator(wrap, gen_cache)
return (letter for letter in alphabet_iter)
def letter_generator(wrap, gen_cache):
delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
letters_ = wrap['letters']
if letters_ not in gen_cache['alphabet']:
gen_cache['alphabet'][letters_] = delegate(letters=letters_)
generator = gen_cache['alphabet'][letters_]
return islice(generator, length)
def code_generator(wrap, gen_cache):
delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
code_ = wrap['code']
if gen_cache['alphabet'][code_] is None:
gen_cache['alphabet'][code_] = delegate(code=code_)
generator = gen_cache['alphabet'][code_]
return islice(generator, length)
def number_generator(wrap, gen_cache):
function_delegate = globals()[wrap['sequence']]
length = int(wrap['length'])
function_name = wrap['sequence']
if gen_cache[function_name] is None:
gen_cache[function_name] = function_delegate()
generator = gen_cache[function_name]
number_iter = islice(generator, length)
return (number for number in number_iter)