Решение на Генератори и итератори от Мартин Филипов

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

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

Резултати

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

Код

def fibonacci():
current = 1
first = 0
second = 1
while True:
yield current
current = first + second
first = second
second = current
def primes():
def is_prime(tested_number):
divisor = 3
while True:
quotient = tested_number // divisor
if quotient < divisor:
return True
if tested_number == quotient * divisor:
return False
divisor += 2
def get_next_prime_number(number):
if number == 2:
return 3
number += 2
while not is_prime(number):
number += 2
return number
current = 2
while True:
yield current
current = get_next_prime_number(current)
def alphabet(**kwargs):
if "letters" in kwargs:
for letter in kwargs["letters"]:
yield letter
if "code" in kwargs and "letters" not in kwargs:
if kwargs["code"] == "lat":
current_letter = ord("a")
last_letter = ord("z")
if kwargs["code"] == "bg":
current_letter = ord("а")
last_letter = ord("я")
while current_letter <= last_letter:
yield chr(current_letter)
if current_letter == ord("ъ") or current_letter == ord("ь"):
current_letter += 1
current_letter += 1
def intertwined_sequences(sequences_container, generator_definitions={}):
unique_generators = dict()
for sequence in sequences_container:
if sequence["sequence"] not in generator_definitions:
generator_definitions[sequence.get("sequence")] = globals()[
sequence.get("sequence")]
generator_arguments = dict()
for key, value in sequence.items():
if key != "sequence" and key != "length":
generator_arguments[key] = value
generator = generator_definitions.get(sequence["sequence"])
length = sequence.get("length")
if sequence.get("sequence") not in unique_generators:
if generator_arguments:
unique_generators[sequence.get("sequence")] = \
generator(**generator_arguments)
else:
unique_generators[sequence.get("sequence")] = generator()
for _ in range(length):
yield next(unique_generators[sequence.get("sequence")])

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

......EE......
======================================================================
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

----------------------------------------------------------------------
Ran 14 tests in 4.183s

FAILED (errors=2)

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

Мартин обнови решението на 03.04.2015 16:27 (преди над 9 години)

+def fibonacci():
+ current = 1
+ first = 0
+ second = 1
+ while True:
+ yield current
+ current = first + second
+ first = second
+ second = current
+
+
+def primes():
+ def is_prime(tested_number):
+ divisor = 3
+ while True:
+ quotient = tested_number // divisor
+ if quotient < divisor:
+ return True
+ if tested_number == quotient * divisor:
+ return False
+ divisor += 2
+
+ def get_next_prime_number(number):
+ if number == 2:
+ return 3
+ number += 2
+ while not is_prime(number):
+ number += 2
+ return number
+
+ current = 2
+ while True:
+ yield current
+ current = get_next_prime_number(current)
+
+
+def alphabet(**kwargs):
+ for key, value in kwargs.items():
+ if key == "letters":
+ for letter in value:
+ yield letter
+ break
+ if key == "code":
+ if value == "lat":
+ current_letter = ord("a")
+ last_letter = ord("z")
+ if value == "bg":
+ current_letter = ord("а")
+ last_letter = ord("я")
+ while current_letter <= last_letter:
+ yield chr(current_letter)
+ if current_letter == ord("ъ") or current_letter == ord("ь"):
+ current_letter += 1
+ current_letter += 1
+
+
+def intertwined_sequences(sequences_container, generator_definitions={}):
+ unique_generators = dict()
+ for sequence in sequences_container:
+ if sequence["sequence"] not in generator_definitions:
+ generator_definitions[sequence.get("sequence")] = globals()[
+ sequence.get("sequence")]
+ generator_arguments = dict()
+ for key, value in sequence.items():
+ if key != "sequence" and key != "length":
+ generator_arguments[key] = value
+ generator = generator_definitions.get(sequence["sequence"])
+ length = sequence.get("length")
+ if sequence.get("sequence") not in unique_generators:
+ if generator_arguments:
+ unique_generators[sequence.get("sequence")] = \
+ generator(**generator_arguments)
+ else:
+ unique_generators[sequence.get("sequence")] = generator()
+ for _ in range(length):
+ yield next(unique_generators[sequence.get("sequence")])

Мартин обнови решението на 03.04.2015 16:42 (преди над 9 години)

def fibonacci():
current = 1
first = 0
second = 1
while True:
yield current
current = first + second
first = second
second = current
def primes():
def is_prime(tested_number):
divisor = 3
while True:
quotient = tested_number // divisor
if quotient < divisor:
return True
if tested_number == quotient * divisor:
return False
divisor += 2
def get_next_prime_number(number):
if number == 2:
return 3
number += 2
while not is_prime(number):
number += 2
return number
current = 2
while True:
yield current
current = get_next_prime_number(current)
def alphabet(**kwargs):
- for key, value in kwargs.items():
- if key == "letters":
- for letter in value:
- yield letter
- break
- if key == "code":
- if value == "lat":
- current_letter = ord("a")
- last_letter = ord("z")
- if value == "bg":
- current_letter = ord("а")
- last_letter = ord("я")
- while current_letter <= last_letter:
- yield chr(current_letter)
- if current_letter == ord("ъ") or current_letter == ord("ь"):
- current_letter += 1
+ if "letters" in kwargs:
+ for letter in kwargs["letters"]:
+ yield letter
+ if "code" in kwargs and "letters" not in kwargs:
+ if kwargs["code"] == "lat":
+ current_letter = ord("a")
+ last_letter = ord("z")
+ if kwargs["code"] == "bg":
+ current_letter = ord("а")
+ last_letter = ord("я")
+ while current_letter <= last_letter:
+ yield chr(current_letter)
+ if current_letter == ord("ъ") or current_letter == ord("ь"):
current_letter += 1
+ current_letter += 1
def intertwined_sequences(sequences_container, generator_definitions={}):
unique_generators = dict()
for sequence in sequences_container:
if sequence["sequence"] not in generator_definitions:
generator_definitions[sequence.get("sequence")] = globals()[
sequence.get("sequence")]
generator_arguments = dict()
for key, value in sequence.items():
if key != "sequence" and key != "length":
generator_arguments[key] = value
generator = generator_definitions.get(sequence["sequence"])
length = sequence.get("length")
if sequence.get("sequence") not in unique_generators:
if generator_arguments:
unique_generators[sequence.get("sequence")] = \
generator(**generator_arguments)
else:
unique_generators[sequence.get("sequence")] = generator()
for _ in range(length):
yield next(unique_generators[sequence.get("sequence")])