Решение на Генератори и итератори от Калоян Кацаров

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

Към профила на Калоян Кацаров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 10 успешни тест(а)
  • 4 неуспешни тест(а)

Код

import math
from itertools import islice
import itertools
from itertools import cycle
from types import GeneratorType
def fibonacci():
a, b = 0, 1
while True:
if a is not 0:
yield a
b = a+b
yield b
a = a+b
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
def primes():
i = 1
while True:
i = i+1
if is_prime(i):
yield i
def alphabet(code, letters=None):
if letters is not None:
return (x for x in letters)
else:
if code is 'lat':
return (chr(x) for x in range(97, 123))
elif code is 'bg':
return (chr(x) for x in range(1072, 1104))
def get_arg(generator_map):
for key, value in generator_map.items():
if key is not 'sequence' and key is not 'length':
return value
def put_my_functions(generators, generator_definitions={}):
for generator in generators:
name = generator['sequence']
if name not in generator_definitions:
if name is 'fibonacci':
generator_definitions[name] = fibonacci
if name is 'primes':
generator_definitions[name] = primes
if name is 'alphabet':
generator_definitions[name] = alphabet
return generator_definitions
def put_known_generator(generators, generator_list, generator_definitions):
for generator in generators:
name = generator['sequence']
if name not in generator_list:
tmp_instance = None
if len(generator) is 2:
tmp_instance = generator_definitions[name]()
else:
tmp_instance = generator_definitions[name](get_arg(generator))
if isinstance(tmp_instance, GeneratorType):
generator_list[name] = tmp_instance
else:
generator_list[name] = (x for x in tmp_instance)
return generator_list
def intertwined_sequences(generators, generator_definitions={}):
generator_list = {}
is_generator = True
try:
len(generators)
except TypeError:
is_generator = False
finally:
if is_generator is True:
generator_definitions = put_my_functions(generators,
generator_definitions)
generator_list = put_known_generator(generators, generator_list,
generator_definitions)
return_result = []
for x in generators:
for y in list(islice(generator_list[x['sequence']],
x['length'])):
return_result.append(y)
for item in return_result:
yield item
else:
endless_generator = generators
while True:
entry = next(endless_generator)
generator_definitions = put_my_functions((entry, ),
generator_definitions)
generator_list = put_known_generator((entry, ), generator_list,
generator_definitions)
for x in range(0, entry['length']):
yield next(generator_list[entry['sequence']])

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

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

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

======================================================================
ERROR: test_glagolitic (test.TestAlphabet)
----------------------------------------------------------------------
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

----------------------------------------------------------------------
Ran 14 tests in 8.185s

FAILED (errors=4)

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

Калоян обнови решението на 02.04.2015 14:25 (преди над 9 години)

+import math
+from itertools import islice
+import itertools
+
+
+def fibonacci():
+ a, b = 0, 1
+ while True:
+ if a is not 0:
+ yield a
+ b = a+b
+ yield b
+ a = a+b
+
+
+def is_prime(number):
+ if number > 1:
+ if number == 2:
+ return True
+ if number % 2 == 0:
+ return False
+ for current in range(3, int(math.sqrt(number) + 1), 2):
+ if number % current == 0:
+ return False
+ return True
+ return False
+
+
+def primes():
+ i = 1
+ while True:
+ i = i+1
+ if is_prime(i):
+ yield i
+
+
+def alphabet(code, letters=None):
+ if letters is not None:
+ return (x for x in letters)
+ else:
+ if code is 'lat':
+ return (chr(x) for x in range(97, 123))
+ elif code is 'bg':
+ return (chr(x) for x in range(1072, 1104))
+
+
+def get_arg(generator_map):
+ for key, value in generator_map.items():
+ if key is not 'sequence' and key is not 'length':
+ return value
+
+
+def intertwined_sequences(generators, generator_definitions=None):
+ generator_list = {}
+ for generator in generators:
+ name = generator['sequence']
+ if name not in generator_list:
+ if name in globals():
+ if len(generator) is 2:
+ generator_list[name] = globals()[name]()
+ else:
+ generator_list[name] = globals()[name](get_arg(generator))
+ else:
+ generator_list[name] = (x for x in
+ generator_definitions[name]())
+ result = [list(islice(generator_list[x['sequence']], x['length']))
+ for x in generators]
+ return list(itertools.chain.from_iterable(result))

Калоян обнови решението на 03.04.2015 12:36 (преди над 9 години)

import math
from itertools import islice
import itertools
+from itertools import cycle
+from types import GeneratorType
def fibonacci():
a, b = 0, 1
while True:
if a is not 0:
yield a
b = a+b
yield b
a = a+b
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
def primes():
i = 1
while True:
i = i+1
if is_prime(i):
yield i
def alphabet(code, letters=None):
if letters is not None:
return (x for x in letters)
else:
if code is 'lat':
return (chr(x) for x in range(97, 123))
elif code is 'bg':
return (chr(x) for x in range(1072, 1104))
def get_arg(generator_map):
for key, value in generator_map.items():
if key is not 'sequence' and key is not 'length':
return value
-def intertwined_sequences(generators, generator_definitions=None):
- generator_list = {}
+def put_my_functions(generators, generator_definitions={}):
for generator in generators:
name = generator['sequence']
+ if name not in generator_definitions:
+ if name is 'fibonacci':
+ generator_definitions[name] = fibonacci
+ if name is 'primes':
+ generator_definitions[name] = primes
+ if name is 'alphabet':
+ generator_definitions[name] = alphabet
+ return generator_definitions
+
+
+def put_known_generator(generators, generator_list, generator_definitions):
+ for generator in generators:
+ name = generator['sequence']
if name not in generator_list:
- if name in globals():
- if len(generator) is 2:
- generator_list[name] = globals()[name]()
- else:
- generator_list[name] = globals()[name](get_arg(generator))
+
+ tmp_instance = None
+
+ if len(generator) is 2:
+ tmp_instance = generator_definitions[name]()
else:
- generator_list[name] = (x for x in
- generator_definitions[name]())
+ tmp_instance = generator_definitions[name](get_arg(generator))
- result = [list(islice(generator_list[x['sequence']], x['length']))
+
- for x in generators]
+ if isinstance(tmp_instance, GeneratorType):
- return list(itertools.chain.from_iterable(result))
+ generator_list[name] = tmp_instance
+ else:
+ generator_list[name] = (x for x in tmp_instance)
+ return generator_list
+
+
+def intertwined_sequences(generators, generator_definitions={}):
+ generator_list = {}
+
+ is_generator = True
+ try:
+ len(generators)
+ except TypeError:
+ is_generator = False
+ finally:
+ if is_generator is True:
+ generator_definitions = put_my_functions(generators,
+ generator_definitions)
+
+ generator_list = put_known_generator(generators, generator_list,
+ generator_definitions)
+
+ return_result = []
+
+ for x in generators:
+ for y in list(islice(generator_list[x['sequence']],
+ x['length'])):
+ return_result.append(y)
+
+ return return_result

Калоян обнови решението на 03.04.2015 12:57 (преди над 9 години)

import math
from itertools import islice
import itertools
from itertools import cycle
from types import GeneratorType
def fibonacci():
a, b = 0, 1
while True:
if a is not 0:
yield a
b = a+b
yield b
a = a+b
def is_prime(number):
if number > 1:
if number == 2:
return True
if number % 2 == 0:
return False
for current in range(3, int(math.sqrt(number) + 1), 2):
if number % current == 0:
return False
return True
return False
def primes():
i = 1
while True:
i = i+1
if is_prime(i):
yield i
def alphabet(code, letters=None):
if letters is not None:
return (x for x in letters)
else:
if code is 'lat':
return (chr(x) for x in range(97, 123))
elif code is 'bg':
return (chr(x) for x in range(1072, 1104))
def get_arg(generator_map):
for key, value in generator_map.items():
if key is not 'sequence' and key is not 'length':
return value
def put_my_functions(generators, generator_definitions={}):
for generator in generators:
name = generator['sequence']
if name not in generator_definitions:
if name is 'fibonacci':
generator_definitions[name] = fibonacci
if name is 'primes':
generator_definitions[name] = primes
if name is 'alphabet':
generator_definitions[name] = alphabet
return generator_definitions
def put_known_generator(generators, generator_list, generator_definitions):
for generator in generators:
name = generator['sequence']
if name not in generator_list:
tmp_instance = None
if len(generator) is 2:
tmp_instance = generator_definitions[name]()
else:
tmp_instance = generator_definitions[name](get_arg(generator))
if isinstance(tmp_instance, GeneratorType):
generator_list[name] = tmp_instance
else:
generator_list[name] = (x for x in tmp_instance)
return generator_list
def intertwined_sequences(generators, generator_definitions={}):
generator_list = {}
is_generator = True
try:
len(generators)
except TypeError:
is_generator = False
finally:
if is_generator is True:
generator_definitions = put_my_functions(generators,
generator_definitions)
generator_list = put_known_generator(generators, generator_list,
generator_definitions)
return_result = []
for x in generators:
for y in list(islice(generator_list[x['sequence']],
x['length'])):
return_result.append(y)
- return return_result
+ for item in return_result:
+ yield item
+ else:
+ endless_generator = generators
+ while True:
+ entry = next(endless_generator)
+ generator_definitions = put_my_functions((entry, ),
+ generator_definitions)
+ generator_list = put_known_generator((entry, ), generator_list,
+ generator_definitions)
+ for x in range(0, entry['length']):
+ yield next(generator_list[entry['sequence']])