Решение на Генератори и итератори от Божидар Иванов

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

Към профила на Божидар Иванов

Резултати

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

Код

import math
lat_alphabet = 'abcdefghijklmnopqrstuvwxyz'
bg_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
def fibonacci():
temporary = 1
element = 1
yield temporary
while True:
temporary, element = element, temporary + element
yield temporary
def is_prime(number):
limit = int(math.sqrt(number)+1)
for index in range(2,limit):
if number % index == 0:
return False
return True
def primes():
temporary = 2;
while True:
if is_prime(temporary):
yield temporary
temporary += 1
def alphabet(**kwargs):
if kwargs.get('letters'):
for index in range(0,len(kwargs.get('letters'))):
yield (kwargs.get('letters'))[index]
if kwargs.get('code') == 'lat':
for index in range(0,len(lat_alphabet)):
yield lat_alphabet[index]
elif kwargs.get('code') == 'bg':
for index in range(0,len(bg_alphabet)):
yield bg_alphabet[index]
def intertwined_sequences(*args,**kwargs):
elements = []
sequences = {}
custom_gens = kwargs.get('generator_definitions')
for params in args:
for param in params:
name = param.pop('sequence')
iterations = param.pop('length')
sequence = sequences.get(name)
if sequence is None:
func = globals().get(name)
if func is None:
func = custom_gens.get(name)
if len(param) == 0:
sequence = iter(func())
else:
sequence = iter(func(**param))
sequences[name] = sequence
for index in range(0,iterations):
elements.append(next(sequence))
return elements

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

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

FAILED (errors=2)

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

Божидар обнови решението на 03.04.2015 14:37 (преди над 9 години)

+import math
+
+
+lat_alphabet = 'abcdefghijklmnopqrstuvwxyz'
+bg_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+
+def fibonacci():
+ temporary = 1
+ element = 1
+ yield temporary
+ while True:
+ temporary, element = element, temporary + element
+ yield temporary
+
+
+def is_prime(number):
+ limit = int(math.sqrt(number)+1)
+ for index in range(2,limit):
+ if number % index == 0:
+ return False
+ return True
+
+
+def primes():
+ temporary = 2;
+ while True:
+ if is_prime(temporary):
+ yield temporary
+ temporary += 1
+
+
+def alphabet(**kwargs):
+
+ if kwargs.get('letters'):
+ for index in range(0,len(kwargs.get('letters'))):
+ yield (kwargs.get('letters'))[index]
+
+ if kwargs.get('code') == 'lat':
+ for index in range(0,len(lat_alphabet)):
+ yield lat_alphabet[index]
+ elif kwargs.get('code') == 'bg':
+ for index in range(0,len(bg_alphabet)):
+ yield bg_alphabet[index]
+
+
+def intertwined_sequences(*args,**kwargs):
+ elements = []
+ sequences = {}
+ custom_gens = kwargs.get('generator_definitions')
+ for params in args:
+ for param in params:
+ name = param.pop('sequence')
+ iterations = param.pop('length')
+ sequence = sequences.get(name)
+ if sequence is None:
+ func = globals().get(name)
+ if func is None:
+ func = custom_gens.get(name)
+ if len(param) == 0:
+ sequence = iter(func())
+ else:
+ sequence = iter(func(**param))
+ sequences[name] = sequence
+
+ for index in range(0,iterations):
+ elements.append(next(sequence))
+ return elements