Решение на Генератори и итератори от Цвета Кандиларова

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

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

Резултати

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

Код

import itertools
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
def is_prime(n):
if n < 2:
return False
return bool(not (len([i for i in range(2, n // 2 + 1) if n % i == 0])))
return (i for i in itertools.count(2) if is_prime(i))
def alphabet(**kwargs):
if 'letters' in kwargs:
return (ch for ch in kwargs['letters'])
else:
if kwargs['code'] == 'lat':
return (ch for ch in 'abcdefghijklmnopqrstuvwxyz')
else:
return (ch for ch in 'абвгдежзийклмнопрстуфхцчшщъьюя')
def intertwined_sequences(iterables, **kwargs):
iters = {}
functions = {
'primes': primes, 'fibonacci': fibonacci, 'alphabet': alphabet
}
for i in iterables:
if i['sequence'] not in iters:
if i['sequence'] not in functions:
attributes = {
key: i[key] for key in i
if key != 'sequence' and key != 'length'}
iters[i['sequence']] = iter(
kwargs['generator_definitions'][i['sequence']](
**attributes))
elif i['sequence'] is not 'alphabet':
iters[i['sequence']] = iter(functions[i['sequence']]())
else:
if 'code' in i:
iters[i['sequence']] = iter(
functions[i['sequence']](code=i['code']))
else:
iters[i['sequence']] = iter(
functions[i['sequence']](letters=i['letters']))
for _ in range(i['length']):
yield next(iters[i['sequence']])

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

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

FAILED (errors=1)

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

Цвета обнови решението на 01.04.2015 01:17 (преди над 9 години)

+import itertools
+
+
+def fibonacci():
+ a, b = 0, 0
+ if a == 0:
+ a += 1
+ while True:
+ b += a
+ a = b - a
+ yield b
+
+
+def primes():
+ def is_prime(n):
+ if n < 2:
+ return False
+ return bool(not (len([i for i in range(2, n // 2 + 1) if n % i == 0])))
+ return (i for i in itertools.count(2) if is_prime(i))
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ return (ch for ch in kwargs['letters'])
+ else:
+ if kwargs['code'] == 'lat':
+ return (ch for ch in 'abcdefghijklmnopqrstuvwhyz')
+ else:
+ return (ch for ch in 'абвгдежзийклмнопрстуфхцчщщъьюя')
+
+
+def intertwined_sequences(generators, **kwargs):
+ iters = {}
+
+ for g in generators:
+ if g['sequence'] not in ['primes', 'fibonacci', 'alphabet']:
+ attributes = {key: g[key]
+ for key in g if key != 'sequence' and key != 'length'}
+ iters[g['sequence']] = iter(
+ kwargs['generator_definitions'][g['sequence']](
+ **attributes))
+ else:
+ if not g['sequence'] in iters:
+ if g['sequence'] != 'alphabet':
+ iters[g['sequence']] = iter(globals()[g['sequence']]())
+ else:
+ if 'code' in g:
+ iters[g['sequence']] = iter(
+ globals()[g['sequence']](code=g['code']))
+ else:
+ iters[g['sequence']] = iter(
+ globals()[g['sequence']](letters=g['letters']))
+
+ for _ in range(g['length']):
+ yield next(iters[g['sequence']])

Цвета обнови решението на 01.04.2015 01:44 (преди над 9 години)

import itertools
def fibonacci():
- a, b = 0, 0
- if a == 0:
- a += 1
+ a, b = 1, 1
while True:
- b += a
- a = b - a
- yield b
+ yield a
+ a, b = b, a + b
def primes():
def is_prime(n):
if n < 2:
return False
return bool(not (len([i for i in range(2, n // 2 + 1) if n % i == 0])))
return (i for i in itertools.count(2) if is_prime(i))
def alphabet(**kwargs):
if 'letters' in kwargs:
return (ch for ch in kwargs['letters'])
else:
if kwargs['code'] == 'lat':
return (ch for ch in 'abcdefghijklmnopqrstuvwhyz')
else:
return (ch for ch in 'абвгдежзийклмнопрстуфхцчщщъьюя')
def intertwined_sequences(generators, **kwargs):
iters = {}
for g in generators:
- if g['sequence'] not in ['primes', 'fibonacci', 'alphabet']:
- attributes = {key: g[key]
- for key in g if key != 'sequence' and key != 'length'}
- iters[g['sequence']] = iter(
- kwargs['generator_definitions'][g['sequence']](
- **attributes))
- else:
- if not g['sequence'] in iters:
- if g['sequence'] != 'alphabet':
- iters[g['sequence']] = iter(globals()[g['sequence']]())
+ if g['sequence'] not in iters:
+ if g['sequence'] not in ['primes', 'fibonacci', 'alphabet']:
+ attributes = {key: g[key]
+ for key in g if key != 'sequence' and key != 'length'
+ }
+ iters[g['sequence']] = iter(
+ kwargs['generator_definitions'][g['sequence']](
+ **attributes))
+
+ elif g['sequence'] is not 'alphabet':
+ iters[g['sequence']] = iter(globals()[g['sequence']]())
+ else:
+ if 'code' in g:
+ iters[g['sequence']] = iter(
+ globals()[g['sequence']](code=g['code']))
else:
- if 'code' in g:
- iters[g['sequence']] = iter(
- globals()[g['sequence']](code=g['code']))
- else:
- iters[g['sequence']] = iter(
- globals()[g['sequence']](letters=g['letters']))
+ iters[g['sequence']] = iter(
+ globals()[g['sequence']](letters=g['letters']))
for _ in range(g['length']):
yield next(iters[g['sequence']])

Цвета обнови решението на 02.04.2015 01:03 (преди над 9 години)

import itertools
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
def is_prime(n):
if n < 2:
return False
return bool(not (len([i for i in range(2, n // 2 + 1) if n % i == 0])))
return (i for i in itertools.count(2) if is_prime(i))
def alphabet(**kwargs):
if 'letters' in kwargs:
return (ch for ch in kwargs['letters'])
else:
if kwargs['code'] == 'lat':
return (ch for ch in 'abcdefghijklmnopqrstuvwhyz')
else:
return (ch for ch in 'абвгдежзийклмнопрстуфхцчщщъьюя')
-def intertwined_sequences(generators, **kwargs):
+def intertwined_sequences(iterables, **kwargs):
iters = {}
+ functions = {
+ 'primes': primes, 'fibonacci': fibonacci, 'alphabet': alphabet
+ }
- for g in generators:
- if g['sequence'] not in iters:
- if g['sequence'] not in ['primes', 'fibonacci', 'alphabet']:
- attributes = {key: g[key]
- for key in g if key != 'sequence' and key != 'length'
- }
- iters[g['sequence']] = iter(
- kwargs['generator_definitions'][g['sequence']](
+ for i in iterables:
+ if i['sequence'] not in iters:
+ if i['sequence'] not in functions:
+ attributes = {
+ key: i[key] for key in i
+ if key != 'sequence' and key != 'length'}
+ iters[i['sequence']] = iter(
+ kwargs['generator_definitions'][i['sequence']](
**attributes))
-
- elif g['sequence'] is not 'alphabet':
- iters[g['sequence']] = iter(globals()[g['sequence']]())
+ elif i['sequence'] is not 'alphabet':
+ iters[i['sequence']] = iter(functions[i['sequence']]())
else:
- if 'code' in g:
- iters[g['sequence']] = iter(
- globals()[g['sequence']](code=g['code']))
+ if 'code' in i:
+ iters[i['sequence']] = iter(
+ functions[i['sequence']](code=i['code']))
else:
- iters[g['sequence']] = iter(
- globals()[g['sequence']](letters=g['letters']))
+ iters[i['sequence']] = iter(
+ functions[i['sequence']](letters=i['letters']))
- for _ in range(g['length']):
- yield next(iters[g['sequence']])
+ for _ in range(i['length']):
+ yield next(iters[i['sequence']])

Цвета обнови решението на 02.04.2015 18:50 (преди над 9 години)

import itertools
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
def is_prime(n):
if n < 2:
return False
return bool(not (len([i for i in range(2, n // 2 + 1) if n % i == 0])))
return (i for i in itertools.count(2) if is_prime(i))
def alphabet(**kwargs):
if 'letters' in kwargs:
return (ch for ch in kwargs['letters'])
else:
if kwargs['code'] == 'lat':
- return (ch for ch in 'abcdefghijklmnopqrstuvwhyz')
+ return (ch for ch in 'abcdefghijklmnopqrstuvwxyz')
else:
- return (ch for ch in 'абвгдежзийклмнопрстуфхцчщщъьюя')
+ return (ch for ch in 'абвгдежзийклмнопрстуфхцчшщъьюя')
def intertwined_sequences(iterables, **kwargs):
iters = {}
functions = {
'primes': primes, 'fibonacci': fibonacci, 'alphabet': alphabet
}
for i in iterables:
if i['sequence'] not in iters:
if i['sequence'] not in functions:
attributes = {
key: i[key] for key in i
if key != 'sequence' and key != 'length'}
iters[i['sequence']] = iter(
kwargs['generator_definitions'][i['sequence']](
**attributes))
elif i['sequence'] is not 'alphabet':
iters[i['sequence']] = iter(functions[i['sequence']]())
else:
if 'code' in i:
iters[i['sequence']] = iter(
functions[i['sequence']](code=i['code']))
else:
iters[i['sequence']] = iter(
functions[i['sequence']](letters=i['letters']))
for _ in range(i['length']):
- yield next(iters[i['sequence']])
+ yield next(iters[i['sequence']])