Решение на Генератори и итератори от Любослав Кънев

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

Към профила на Любослав Кънев

Резултати

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

Код

def fibonacci():
first = 1
second = 1
while True:
yield first
first, second = second, first
second += first
def primes():
def is_prime(num):
for i in range(2, num - 1):
if num % i == 0:
return False
return True
number = 1
while True:
number += 1
if is_prime(number):
yield number
def alphabet(code="", letters=None):
if letters is None:
if code == "bg":
for i in range(ord('а'), ord('я') + 1):
if abs(ord('ь') - i) != 1:
yield chr(i)
elif code == "lat":
for i in range(ord('a'), ord('z') + 1):
yield chr(i)
else:
for i in letters:
yield i
def intertwined_sequences(generator_instructions=(), generator_definitions={}):
if 'fibonacci' not in generator_definitions.keys():
generator_definitions['fibonacci'] = fibonacci
if 'primes' not in generator_definitions.keys():
generator_definitions['primes'] = primes
if 'alphabet' not in generator_definitions.keys():
generator_definitions['alphabet'] = alphabet
generator_objects = {}
for instruction in generator_instructions:
key = instruction['sequence']
if key not in generator_objects and key in generator_definitions:
args = {}
for arg, value in instruction.items():
if arg != 'sequence' and arg != 'length':
args[arg] = value
generator_objects[key] = iter(generator_definitions[key](**args))
for i in range(instruction['length']):
yield next(generator_objects[key])

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

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

FAILED (errors=1)

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

Любослав обнови решението на 31.03.2015 13:00 (преди над 9 години)

+def fibonacci():
+ first = 1;
+ second = 1;
+
+ while True:
+ yield first
+ first, second = second, first
+ second += first
+
+
+
+def primes():
+ def is_prime(num):
+ for i in range(2, num - 1):
+ if num % i == 0:
+ return False
+ return True
+
+ i = 1
+ while True:
+ i += 1
+ if is_prime(i):
+ yield i

Любослав обнови решението на 02.04.2015 23:46 (преди над 9 години)

+import types
+
+
def fibonacci():
- first = 1;
- second = 1;
-
+ first = 1
+ second = 1
+
while True:
yield first
first, second = second, first
second += first
-
-
-
+
+
def primes():
def is_prime(num):
for i in range(2, num - 1):
if num % i == 0:
return False
return True
-
- i = 1
+
+ number = 1
while True:
- i += 1
- if is_prime(i):
+ number += 1
- yield i
+ if is_prime(number):
+ yield number
+
+
+def alphabet(code="", letters=None):
+ if letters is None:
+ if code == "bg":
+ for i in range(ord('а'), ord('я') + 1):
+ if abs(ord('ь') - i) != 1:
+ yield chr(i)
+ elif code == "lat":
+ for i in range(ord('a'), ord('z') + 1):
+ yield chr(i)
+ else:
+ for i in letters:
+ yield i
+
+
+def intertwined_sequences(instructions=(), generator_definitions={}):
+ known_generator_definitions = {'fibonacci': fibonacci, 'primes':
+ primes, 'alphabet': alphabet}
+
+ generator_objects = {}
+
+ for definition in instructions:
+ key = definition['sequence']
+
+ if key not in generator_objects:
+ if key in known_generator_definitions.keys():
+ if key == 'alphabet':
+ if 'letters' in definition.keys():
+ generator_objects[key] = \
+ known_generator_definitions[key](
+ letters=definition['letters'])
+ elif 'code' in definition.keys():
+ generator_objects[key] = known_generator_definitions[
+ key](code=definition['code'])
+ else:
+ generator_objects[key] = known_generator_definitions[key]()
+ elif key in generator_definitions.keys():
+ generator_objects[key] = iter(generator_definitions[key]())
+ for i in range(definition['length']):
+ yield next(generator_objects[key])
+

Любослав обнови решението на 03.04.2015 16:46 (преди над 9 години)

-import types
-
-
def fibonacci():
first = 1
second = 1
while True:
yield first
first, second = second, first
second += first
def primes():
def is_prime(num):
for i in range(2, num - 1):
if num % i == 0:
return False
return True
number = 1
while True:
number += 1
if is_prime(number):
yield number
def alphabet(code="", letters=None):
if letters is None:
if code == "bg":
for i in range(ord('а'), ord('я') + 1):
if abs(ord('ь') - i) != 1:
yield chr(i)
elif code == "lat":
for i in range(ord('a'), ord('z') + 1):
yield chr(i)
else:
for i in letters:
yield i
-def intertwined_sequences(instructions=(), generator_definitions={}):
- known_generator_definitions = {'fibonacci': fibonacci, 'primes':
- primes, 'alphabet': alphabet}
+def intertwined_sequences(generator_instructions=(), generator_definitions={}):
+ if 'fibonacci' not in generator_definitions.keys():
+ generator_definitions['fibonacci'] = fibonacci
+ if 'primes' not in generator_definitions.keys():
+ generator_definitions['primes'] = primes
+ if 'alphabet' not in generator_definitions.keys():
+ generator_definitions['alphabet'] = alphabet
generator_objects = {}
- for definition in instructions:
- key = definition['sequence']
+ for instruction in generator_instructions:
+ key = instruction['sequence']
- if key not in generator_objects:
- if key in known_generator_definitions.keys():
- if key == 'alphabet':
- if 'letters' in definition.keys():
- generator_objects[key] = \
- known_generator_definitions[key](
- letters=definition['letters'])
- elif 'code' in definition.keys():
- generator_objects[key] = known_generator_definitions[
- key](code=definition['code'])
- else:
- generator_objects[key] = known_generator_definitions[key]()
- elif key in generator_definitions.keys():
- generator_objects[key] = iter(generator_definitions[key]())
- for i in range(definition['length']):
- yield next(generator_objects[key])
+ if key not in generator_objects and key in generator_definitions:
+ args = {}
+ for arg, value in instruction.items():
+ if arg != 'sequence' and arg != 'length':
+ args[arg] = value
+ generator_objects[key] = iter(generator_definitions[key](**args))
+
+ for i in range(instruction['length']):
+ yield next(generator_objects[key])