Решение на Генератори и итератори от Красимир Николов

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

Към профила на Красимир Николов

Резултати

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

Код

import string
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
num = 2
while True:
for iter_num in range(2, num):
if num % iter_num == 0:
break
else:
yield num
num += 1
def alphabet(code=None, letters=None):
if letters is not None:
letters = letters
else:
if code == 'bg':
ignore_symbols = ['ы', 'э']
letters = [chr(sym)
for sym in range(ord('а'), ord('я') + 1)]
letters = [item for item in letters
if item not in ignore_symbols]
else:
letters = string.ascii_lowercase
for letter in letters:
yield letter
def intertwined_sequences(generator, generator_definitions=None):
call_dict = {}
for gen in generator:
if generator_definitions is not None:
for key, value in generator_definitions.items():
if key == gen['sequence'] and key not in call_dict:
globals()[gen['sequence']] = value
call_func, call_dict = call_func_and_dict_func(gen, call_dict)
for length in range(gen['length']):
yield next(call_func)
def get_params(dictionary, keys):
key_set = set(dictionary.keys()) - set(keys)
return {key: dictionary[key] for key in key_set}
def call_func_and_dict_func(gen, call_dict):
if gen['sequence'] in call_dict:
return call_dict[gen['sequence']], call_dict
exclude_keys = ['sequence', 'length']
params = get_params(gen, exclude_keys)
if params:
call_func = globals()[gen['sequence']](**params)
else:
call_func = globals()[gen['sequence']]()
call_dict[gen['sequence']] = iter(call_func)
return call_dict[gen['sequence']], call_dict

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

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

FAILED (errors=1)

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

Красимир обнови решението на 31.03.2015 09:41 (преди над 9 години)

+import string
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ num = 2
+ while True:
+ for iter_num in range(2, num):
+ if num % iter_num == 0:
+ break
+ else:
+ yield num
+ num += 1
+
+
+def alphabet(code=None, letters=None):
+ if letters is not None:
+ letters = letters
+ else:
+ if code == 'bg':
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ else:
+ letters = string.ascii_lowercase
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(generator, generator_definitions=None):
+ call_dict = {}
+ for gen in generator:
+ if generator_definitions is not None:
+ for key, value in generator_definitions.items():
+ if key == gen['sequence'] and key not in call_dict:
+ globals()[gen['sequence']] = value
+ call_func, call_dict = call_func_and_dict_func(gen, call_dict)
+ for length in range(gen['length']):
+ yield next(call_func)
+
+
+def get_params(dictionary, keys):
+ key_set = set(dictionary.keys()) - set(keys)
+ return {key: dictionary[key] for key in key_set}
+
+
+def call_func_and_dict_func(gen, call_dict):
+ if gen['sequence'] in call_dict:
+ return call_dict[gen['sequence']], call_dict
+ exclude_keys = ['sequence', 'length']
+ params = get_params(gen, exclude_keys)
+ if params:
+ call_func = globals()[gen['sequence']](**params)
+ else:
+ call_func = globals()[gen['sequence']]()
+ call_dict[gen['sequence']] = call_func
+
+ return call_func, call_dict

Красимир обнови решението на 31.03.2015 22:50 (преди над 9 години)

import string
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
num = 2
while True:
for iter_num in range(2, num):
if num % iter_num == 0:
break
else:
yield num
num += 1
def alphabet(code=None, letters=None):
if letters is not None:
letters = letters
else:
if code == 'bg':
- letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ ignore_symbols = ['ы', 'э']
+ letters = [chr(sym)
+ for sym in range(ord('а'), ord('я') + 1)]
+ letters = [item for item in letters
+ if item not in ignore_symbols]
else:
letters = string.ascii_lowercase
for letter in letters:
yield letter
def intertwined_sequences(generator, generator_definitions=None):
call_dict = {}
for gen in generator:
if generator_definitions is not None:
for key, value in generator_definitions.items():
if key == gen['sequence'] and key not in call_dict:
globals()[gen['sequence']] = value
call_func, call_dict = call_func_and_dict_func(gen, call_dict)
for length in range(gen['length']):
yield next(call_func)
def get_params(dictionary, keys):
key_set = set(dictionary.keys()) - set(keys)
return {key: dictionary[key] for key in key_set}
def call_func_and_dict_func(gen, call_dict):
if gen['sequence'] in call_dict:
return call_dict[gen['sequence']], call_dict
exclude_keys = ['sequence', 'length']
params = get_params(gen, exclude_keys)
if params:
call_func = globals()[gen['sequence']](**params)
else:
call_func = globals()[gen['sequence']]()
- call_dict[gen['sequence']] = call_func
+ call_dict[gen['sequence']] = iter(call_func)
- return call_func, call_dict
+ return call_dict[gen['sequence']], call_dict