Решение на Генератори и итератори от Елена Денева

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

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

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 11 успешни тест(а)
  • 3 неуспешни тест(а)

Код

from math import sqrt, floor
from string import ascii_lowercase
from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
return chain(letters or (code == 'lat' and ascii_lowercase) or BULGARIAN)
def intertwined_sequences(sequences, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
def get(sequence, length, **kwargs):
if not(sequence in mem):
mem[sequence] = generator_definitions[sequence](**kwargs)
return islice(mem.get(sequence), 0, length)
return chain(*[get(**s) for s in sequences])

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

......EE....E.
======================================================================
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_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 6.180s

FAILED (errors=3)

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

Елена обнови решението на 30.03.2015 20:49 (преди над 9 години)

+# -*- coding: utf-8 -*-
+
+from math import sqrt, floor
+from string import ascii_lowercase
+from itertools import islice
+
+
+BULGARIAN = u''.join(list(map(unichr, range(ord(u'а'), ord(u'я')+1))))
+
+
+def fibonacci():
+ x, y = 0, 1
+ while True:
+ yield y
+ x, y = y, x+y
+
+
+def primes():
+ x = 2
+ while True:
+ if all(map(lambda i: x % i, range(2, (1 + int(sqrt(x)))))):
+ yield x
+ x = x + 1
+
+
+def alphabet(code=None, letters=None):
+ it = iter(letters or (ascii_lowercase if code == 'lat' else BULGARIAN))
+ while True:
+ try:
+ yield next(it)
+ except StopIteration:
+ return
+
+
+def composite_generator(generators):
+ for times, generator in generators:
+ for _ in range(0, times):
+ yield next(generator)
+
+
+def intertwined_sequences(seqs, generator_definitions={}):
+ generators = {'fibonacci': fibonacci(), 'primes': primes()}
+
+ def create(name, seq):
+ if name == 'alphabet':
+ return alphabet(seq.get("code", None), seq.get("letters", None))
+ return generator_definitions[name](**seq)
+
+ def get(seq):
+ key = seq.get("sequence")
+ if not(key in generators):
+ generators[key] = create(key, seq)
+ return generators.get(key)
+
+ return composite_generator([(seq["length"], get(seq)) for seq in seqs])

Елена обнови решението на 30.03.2015 22:50 (преди над 9 години)

# -*- coding: utf-8 -*-
from math import sqrt, floor
from string import ascii_lowercase
from itertools import islice
+from re import match
+BULGARIAN = [
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
+ 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'
+]
-BULGARIAN = u''.join(list(map(unichr, range(ord(u'а'), ord(u'я')+1))))
-
-def fibonacci():
+def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
-def primes():
+def primes(**kwargs):
x = 2
while True:
- if all(map(lambda i: x % i, range(2, (1 + int(sqrt(x)))))):
+ if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
-def alphabet(code=None, letters=None):
+def alphabet(code=None, letters=None, **kwargs):
it = iter(letters or (ascii_lowercase if code == 'lat' else BULGARIAN))
while True:
try:
yield next(it)
except StopIteration:
return
def composite_generator(generators):
for times, generator in generators:
for _ in range(0, times):
yield next(generator)
def intertwined_sequences(seqs, generator_definitions={}):
- generators = {'fibonacci': fibonacci(), 'primes': primes()}
+ mem = {'fibonacci': fibonacci(), 'primes': primes()}
+ generator_definitions['alphabet'] = alphabet
- def create(name, seq):
- if name == 'alphabet':
- return alphabet(seq.get("code", None), seq.get("letters", None))
- return generator_definitions[name](**seq)
+ def get(seq, length, **kwargs):
+ mem[seq] = mem.get(seq, None) or generator_definitions[seq](**kwargs)
+ return (length, mem.get(seq))
- def get(seq):
- key = seq.get("sequence")
+ return composite_generator([get(s.get("sequence"), **s) for s in seqs])
- if not(key in generators):
- generators[key] = create(key, seq)
- return generators.get(key)
-
- return composite_generator([(seq["length"], get(seq)) for seq in seqs])

Елена обнови решението на 30.03.2015 23:07 (преди над 9 години)

# -*- coding: utf-8 -*-
from math import sqrt, floor
from string import ascii_lowercase
-from itertools import islice
+from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
it = iter(letters or (ascii_lowercase if code == 'lat' else BULGARIAN))
while True:
try:
yield next(it)
except StopIteration:
return
-def composite_generator(generators):
- for times, generator in generators:
- for _ in range(0, times):
- yield next(generator)
-
-
def intertwined_sequences(seqs, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
def get(seq, length, **kwargs):
mem[seq] = mem.get(seq, None) or generator_definitions[seq](**kwargs)
- return (length, mem.get(seq))
+ return islice(mem.get(seq), 0, length)
- return composite_generator([get(s.get("sequence"), **s) for s in seqs])
+ return chain(*[get(s.get("sequence"), **s) for s in seqs])

Елена обнови решението на 30.03.2015 23:12 (преди над 9 години)

# -*- coding: utf-8 -*-
from math import sqrt, floor
from string import ascii_lowercase
from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
- it = iter(letters or (ascii_lowercase if code == 'lat' else BULGARIAN))
- while True:
- try:
- yield next(it)
- except StopIteration:
- return
-
+ return chain(letters or (code == 'lat' and ascii_lowercase) or BULGARIAN)
def intertwined_sequences(seqs, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
def get(seq, length, **kwargs):
mem[seq] = mem.get(seq, None) or generator_definitions[seq](**kwargs)
return islice(mem.get(seq), 0, length)
return chain(*[get(s.get("sequence"), **s) for s in seqs])

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

# -*- coding: utf-8 -*-
from math import sqrt, floor
from string import ascii_lowercase
from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
return chain(letters or (code == 'lat' and ascii_lowercase) or BULGARIAN)
+
def intertwined_sequences(seqs, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
def get(seq, length, **kwargs):
mem[seq] = mem.get(seq, None) or generator_definitions[seq](**kwargs)
return islice(mem.get(seq), 0, length)
return chain(*[get(s.get("sequence"), **s) for s in seqs])
  • Ако имаш нужда от # -*- coding: utf-8 -*-, за да работи кода ти, значи използваш python2, което е проблем.
  • sequences е хубаво име, нищо не печелиш от съкращаването до seqs.
  • Имаш две разменени букви в българската азкуба.
  • В наякои случаи не подаваш keyword аргументите правилно

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

-# -*- coding: utf-8 -*-
-
from math import sqrt, floor
from string import ascii_lowercase
from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
- 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ь', 'ъ', 'ю', 'я'
+ 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
return chain(letters or (code == 'lat' and ascii_lowercase) or BULGARIAN)
-def intertwined_sequences(seqs, generator_definitions={}):
+def intertwined_sequences(sequences, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
- def get(seq, length, **kwargs):
- mem[seq] = mem.get(seq, None) or generator_definitions[seq](**kwargs)
- return islice(mem.get(seq), 0, length)
+ def get(sequence, length, **kwargs):
+ mem[sequence] = mem.get(sequence, None) or generator_definitions[sequence](**kwargs)
+ return islice(mem.get(sequence), 0, length)
- return chain(*[get(s.get("sequence"), **s) for s in seqs])
+ return chain(*[get(**s) for s in sequences])

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

from math import sqrt, floor
from string import ascii_lowercase
from itertools import chain, islice
from re import match
BULGARIAN = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к', 'л', 'м', 'н', 'о',
'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
def fibonacci(**kwargs):
x, y = 0, 1
while True:
yield y
x, y = y, x+y
def primes(**kwargs):
x = 2
while True:
if not(match(r'^1?$|^(11+?)\1+$', x*'1')):
yield x
x = x + 1
def alphabet(code=None, letters=None, **kwargs):
return chain(letters or (code == 'lat' and ascii_lowercase) or BULGARIAN)
def intertwined_sequences(sequences, generator_definitions={}):
mem = {'fibonacci': fibonacci(), 'primes': primes()}
generator_definitions['alphabet'] = alphabet
def get(sequence, length, **kwargs):
- mem[sequence] = mem.get(sequence, None) or generator_definitions[sequence](**kwargs)
+ if not(sequence in mem):
+ mem[sequence] = generator_definitions[sequence](**kwargs)
return islice(mem.get(sequence), 0, length)
return chain(*[get(**s) for s in sequences])