Решение на Генератори и итератори от Гален Георгиев

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

Към профила на Гален Георгиев

Резултати

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

Код

import math
BULGARIAN_ALPHABET = [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и',
'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
]
def fibonacci():
a = 0
b = 1
while True:
yield b
c = b
b += a
a = c
def is_prime(num):
for a in range(2, (int)(math.sqrt(num) + 1)):
if num % a == 0:
return False
return True
def primes():
a = 2
while True:
if is_prime(a):
yield a
a += 1
def alphabet(code='', letters=''):
if letters != '':
for letter in letters:
yield letter
return
if code == 'lat':
start = ord('a')
end = ord('z')
while start <= end:
yield chr(start)
start += 1
elif code == 'bg':
for i in BULGARIAN_ALPHABET:
yield i
def intertwined_sequences(sequences, generator_definitions={}):
fib = fibonacci()
prm = primes()
alpha = {}
gen_dict = {}
for key, value in generator_definitions.items():
key_words = {}
for object in sequences:
if object["sequence"] == key:
for key_dict, value_dict in object.items():
if key_dict != "sequence" and key_dict != "length":
key_words[key_dict] = value_dict
gen_dict[key] = iter(value(**key_words))
for object in sequences:
current_sequence = object["sequence"]
size = object["length"]
if current_sequence == "fibonacci":
for i in range(0, size):
yield next(fib)
elif current_sequence == "primes":
for i in range(0, size):
yield next(prm)
elif current_sequence == "alphabet":
if alpha == {}:
if "letters" in object:
temp_alphabet = alphabet(letters=object["letters"])
else:
temp_alphabet = alphabet(code=object["code"])
alpha = temp_alphabet
for i in range(0, size):
yield next(alpha)
else:
for i in range(0, size):
yield next(gen_dict[current_sequence])

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

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

FAILED (errors=1)

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

Гален обнови решението на 31.03.2015 23:37 (преди над 9 години)

+import math
+
+
+BULGARIAN_ALPHABET = [
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и',
+ 'й', 'к', 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+ ]
+
+
+def fibonacci():
+ a = 0
+ b = 1
+
+ while True:
+ yield b
+ c = b
+ b += a
+ a = c
+
+
+def is_prime(num):
+ for a in range(2, (int)(math.sqrt(num) + 1)):
+ if num % a == 0:
+ return False
+
+ return True
+
+
+def primes():
+ a = 2
+
+ while True:
+ if is_prime(a):
+ yield a
+ a += 1
+
+
+def alphabet(code='', letters=''):
+ if letters != '':
+ for letter in letters:
+ yield letter
+ return
+
+ if code == 'lat':
+ start = ord('a')
+ end = ord('z')
+
+ while start <= end:
+ yield chr(start)
+ start += 1
+ elif code == 'bg':
+ for i in BULGARIAN_ALPHABET:
+ yield i
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ fib = fibonacci()
+ prm = primes()
+ alpha = {}
+ gen_dict = {}
+
+ for key, value in generator_definitions.items():
+ key_words = {}
+ for object in sequences:
+ if object["sequence"] == key:
+ for key_dict, value_dict in object.items():
+ if key_dict != "sequence" and key_dict != "length":
+ key_words[key_dict] = value_dict
+
+ gen_dict[key] = iter(value(**key_words))
+
+ for object in sequences:
+ current_sequence = object["sequence"]
+ size = object["length"]
+
+ if current_sequence == "fibonacci":
+ for i in range(0, size):
+ yield next(fib)
+ elif current_sequence == "primes":
+ for i in range(0, size):
+ yield next(prm)
+ elif current_sequence == "alphabet":
+ if alpha == {}:
+ if "letters" in object:
+ temp_alphabet = alphabet(letters=object["letters"])
+ else:
+ temp_alphabet = alphabet(code=object["code"])
+
+ alpha = temp_alphabet
+
+ for i in range(0, size):
+ yield next(alpha)
+ else:
+ for i in range(0, size):
+ yield next(gen_dict[current_sequence])