Решение на Генератори и итератори от Виктор Марков

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

Към профила на Виктор Марков

Резултати

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

Код

def fibonacci():
value1, value2 = 1, 1
while True:
yield value1
value1, value2 = value2, value1 + value2
def is_prime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return False
return True
def primes():
value = 1
while True:
if is_prime(value):
yield value
value = value + 1
ALPHABETS = {
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
"en": "abcdefghijklmnopqrstuvwxyz"
}
def alphabet(letters="", code=""):
alphabet = letters if letters else ALPHABETS[code]
index = 0
while True:
yield alphabet[index % len(alphabet)]
index += 1
generator_mapping = {
"fibonacci": fibonacci,
"primes": primes,
"alphabet": alphabet
}
def intertwined_sequences(iter_object, generator_definitions={}):
generator_mapping.update(generator_definitions)
gens = {}
for itr in iter_object:
args = {k:v for (k,v) in itr.items() if k not in ("sequence", "length")}
if itr["sequence"] not in gens:
gens[itr["sequence"]] = generator_mapping[itr["sequence"]](**args)
for i in range(0, itr["length"]):
yield (next(iter(gens[itr["sequence"]])))

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

.E.E..EEEE.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_lat_alphabet (test.TestAlphabet)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_generator_definitions (test.TestIntertwine)
----------------------------------------------------------------------
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_intertwine (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 16.195s

FAILED (errors=8)

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

Виктор обнови решението на 03.04.2015 15:10 (преди над 9 години)

+def fibonacci():
+ value1, value2 = 1, 1
+ while True:
+ yield value1
+ value1, value2 = value2, value1 + value2
+
+
+def is_prime(n):
+ for i in range(2,int(n**0.5)+1):
+ if n%i==0:
+ return False
+ return True
+
+
+def primes():
+ value = 1
+ while True:
+ if is_prime(value):
+ yield value
+ value = value + 1
+
+
+ALPHABETS = {
+ "bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
+ "en": "abcdefghijklmnopqrstuvwxyz"
+}
+
+
+def alphabet(letters="", code=""):
+ alphabet = letters if letters else ALPHABETS[code]
+ index = 0
+ while True:
+ yield alphabet[index % len(alphabet)]
+ index += 1
+
+
+generator_mapping = {
+ "fibonacci": fibonacci,
+ "primes": primes,
+ "alphabet": alphabet
+}
+
+
+def intertwined_sequences(iter_object, generator_definitions={}):
+ generator_mapping.update(generator_definitions)
+ gens = {}
+ for itr in iter_object:
+ args = {k:v for (k,v) in itr.items() if k not in ("sequence", "length")}
+ if itr["sequence"] not in gens:
+ gens[itr["sequence"]] = generator_mapping[itr["sequence"]](**args)
+ for i in range(0, itr["length"]):
+ yield (next(iter(gens[itr["sequence"]])))