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

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

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

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 11 успешни тест(а)
  • 3 неуспешни тест(а)

Код

def fibonacci():
fib = 1
fib1 = 1
while True:
yield fib
fib, fib1 = fib1, fib + fib1
def is_prime(a):
if a == 1:
return False
for i in (range(2, a)):
if a % i == 0:
return False
return True
def primes():
a = 1
while True:
if is_prime(a):
yield a
a += 1
def alphabet_gen(string):
while True:
for char in string:
yield char
def alphabet(code="", letters=""):
if code == "bg":
bgalphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
return alphabet_gen(bgalphabet)
if code == "lat":
latinalphabet = "abcdefghijklmnopqrstuvxyz"
return alphabet_gen(latinalphabet)
if code != "" and letters != "":
return alphabet_gen(letters)
if letters != "":
return alphabet_gen(letters)
def intertwined_sequences(iter_object, generator_definitions={}):
generators = {}
for dic in iter_object:
if dic['sequence'] not in generators:
access = globals().copy()
access.update(locals())
current = None
if len(dic) <= 2:
if dic['sequence'] in generator_definitions:
current = iter(generator_definitions[dic['sequence']]())
else:
current = access.get(dic['sequence'])()
i = 0
while i < dic['length']:
yield next(current)
i += 1
generators[dic['sequence']] = current
else:
parametars = {}
for key in dic:
if key != 'sequence' and key != 'length':
parametars[key] = dic[key]
current = access.get(dic['sequence'])(**parametars)
i = 0
while i < dic['length']:
yield next(current)
i += 1
generators[dic['sequence']] = current
else:
i = 0
while i < dic['length']:
yield next(generators[dic['sequence']])
i += 1

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

...E...E..E...
======================================================================
ERROR: test_lat_alphabet (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

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

----------------------------------------------------------------------
Ran 14 tests in 7.463s

FAILED (errors=3)

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

Галин обнови решението на 03.04.2015 16:32 (преди над 9 години)

+def fibonacci():
+ fib = 1
+ fib1 = 1
+ while True:
+ yield fib
+ fib, fib1 = fib1, fib + fib1
+
+
+def is_prime(a):
+ if a == 1:
+ return False
+ for i in (range(2, a)):
+ if a % i == 0:
+ return False
+ return True
+
+
+def primes():
+ a = 1
+ while True:
+ if is_prime(a):
+ yield a
+ a += 1
+
+
+def alphabet_gen(string):
+ while True:
+ for char in string:
+ yield char
+
+
+def alphabet(code="", letters=""):
+ if code == "bg":
+ bgalphabet = "абвгдежзийклмнопрстуфхцчшщъьюя"
+ return alphabet_gen(bgalphabet)
+ if code == "lat":
+ latinalphabet = "abcdefghijklmnopqrstuvxyz"
+ return alphabet_gen(latinalphabet)
+ if code != "" and letters != "":
+ return alphabet_gen(letters)
+ if letters != "":
+ return alphabet_gen(letters)
+
+
+def intertwined_sequences(iter_object, generator_definitions={}):
+ generators = {}
+ for dic in iter_object:
+ if dic['sequence'] not in generators:
+ access = globals().copy()
+ access.update(locals())
+ current = None
+ if len(dic) <= 2:
+ if dic['sequence'] in generator_definitions:
+ current = iter(generator_definitions[dic['sequence']]())
+ else:
+ current = access.get(dic['sequence'])()
+ i = 0
+ while i < dic['length']:
+ yield next(current)
+ i += 1
+ generators[dic['sequence']] = current
+ else:
+ parametars = {}
+ for key in dic:
+ if key != 'sequence' and key != 'length':
+ parametars[key] = dic[key]
+ current = access.get(dic['sequence'])(**parametars)
+ i = 0
+ while i < dic['length']:
+ yield next(current)
+ i += 1
+ generators[dic['sequence']] = current
+ else:
+ i = 0
+ while i < dic['length']:
+ yield next(generators[dic['sequence']])
+ i += 1