Решение на Генератори и итератори от Диан Тодоров

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

Към профила на Диан Тодоров

Резултати

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

Код

from itertools import cycle
import math
def fibonacci():
f1, f2 = 1, 1
while True:
yield f1
f1, f2 = f2, f1 + f2
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
for i in range(2, int(math.sqrt(n) + 1)):
if n % i == 0:
return False
return n > 1
def alphabet(letters=None, code=None):
if letters is not None:
return letters
elif code is not None:
if code == 'lat':
return (chr(n) for n in range(ord('a'), ord('z') + 1))
elif code == 'bg':
return (chr(n) for n in range(ord('а'), ord('я') + 1) if n != ord('ы') and n != ord('э'))
def intertwined_sequences(sequence_specifications, generator_definitions=None):
shortcuts = {'fibonacci': fibonacci(),
'primes': primes()}
for current_sequence_specification in sequence_specifications:
if current_sequence_specification['sequence'] not in shortcuts:
additional_arguments = {key: value for key, value in current_sequence_specification.items() if
key != 'sequence' and key != 'length'}
if current_sequence_specification['sequence'] == 'alphabet':
shortcuts['alphabet'] = alphabet(**additional_arguments)
elif any(additional_arguments):
shortcuts[current_sequence_specification['sequence']] = iter(
generator_definitions[current_sequence_specification['sequence']](**additional_arguments))
else:
shortcuts[current_sequence_specification['sequence']] = iter(
generator_definitions[current_sequence_specification['sequence']]())
current_sequence = shortcuts[current_sequence_specification['sequence']]
for _ in range(0, current_sequence_specification['length']):
yield next(current_sequence)

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

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

----------------------------------------------------------------------
Ran 14 tests in 2.203s

FAILED (errors=1)

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

Диан обнови решението на 01.04.2015 15:51 (преди над 9 години)

+import math
+
+
+def fibonacci():
+ f1, f2 = 1, 1
+ while True:
+ yield f1
+ f1, f2 = f2, f1 + f2
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def is_prime(n):
+ for i in range(2, int(math.sqrt(n) + 1)):
+ if n % i == 0:
+ return False
+ return n > 1
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ return (n for n in kwargs['letters'])
+ elif 'code' in kwargs:
+ if kwargs['code'] == 'lat':
+ return (chr(n) for n in range(ord('a'), ord('z') + 1))
+ elif kwargs['code'] == 'bg':
+ return (chr(n) for n in range(ord('а'), ord('я') + 1) if n != ord('ы') and n != ord('э'))
+
+
+from itertools import chain, islice
+
+
+def intertwined_sequences(*args, **kwargs):
+ generators_items = args[0]
+ generators_shortcuts = {'fibonacci': fibonacci(),
+ 'primes': primes(),
+ 'alphabet': alphabet}
+
+ result_generator = chain()
+ if 'generator_definitions' in kwargs:
+ generators_shortcuts.update(kwargs['generator_definitions'])
+
+ for current_generator_item in generators_items:
+ result_generator = chain(result_generator, islice(generators_shortcuts[current_generator_item['sequence']](),
+ current_generator_item['length']))
+
+ return result_generator
+
+

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

+from itertools import cycle
import math
def fibonacci():
f1, f2 = 1, 1
while True:
yield f1
f1, f2 = f2, f1 + f2
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def is_prime(n):
for i in range(2, int(math.sqrt(n) + 1)):
if n % i == 0:
return False
return n > 1
-def alphabet(**kwargs):
- if 'letters' in kwargs:
- return (n for n in kwargs['letters'])
- elif 'code' in kwargs:
- if kwargs['code'] == 'lat':
+def alphabet(letters=None, code=None):
+ if letters is not None:
+ return letters
+ elif code is not None:
+ if code == 'lat':
return (chr(n) for n in range(ord('a'), ord('z') + 1))
- elif kwargs['code'] == 'bg':
+ elif code == 'bg':
return (chr(n) for n in range(ord('а'), ord('я') + 1) if n != ord('ы') and n != ord('э'))
-from itertools import chain, islice
+def intertwined_sequences(sequence_specifications, generator_definitions=None):
+ shortcuts = {'fibonacci': fibonacci(),
+ 'primes': primes()}
+ for current_sequence_specification in sequence_specifications:
-def intertwined_sequences(*args, **kwargs):
- generators_items = args[0]
- generators_shortcuts = {'fibonacci': fibonacci(),
- 'primes': primes(),
- 'alphabet': alphabet}
+ if current_sequence_specification['sequence'] not in shortcuts:
+ additional_arguments = {key: value for key, value in current_sequence_specification.items() if
+ key != 'sequence' and key != 'length'}
- result_generator = chain()
- if 'generator_definitions' in kwargs:
- generators_shortcuts.update(kwargs['generator_definitions'])
+ if current_sequence_specification['sequence'] == 'alphabet':
+ shortcuts['alphabet'] = alphabet(**additional_arguments)
- for current_generator_item in generators_items:
- result_generator = chain(result_generator, islice(generators_shortcuts[current_generator_item['sequence']](),
- current_generator_item['length']))
+ elif any(additional_arguments):
+ shortcuts[current_sequence_specification['sequence']] = iter(
+ generator_definitions[current_sequence_specification['sequence']](**additional_arguments))
+ else:
+ shortcuts[current_sequence_specification['sequence']] = iter(
+ generator_definitions[current_sequence_specification['sequence']]())
- return result_generator
+ current_sequence = shortcuts[current_sequence_specification['sequence']]
+
+ for _ in range(0, current_sequence_specification['length']):
+ yield next(current_sequence)