Решение на Генератори и итератори от Христиан Димитров

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

Към профила на Христиан Димитров

Резултати

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

Код

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
def alphabet(*, code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
for sequence in sequences:
has_parameter = False
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
if alphabet_sequence == []:
if "code" not in sequence.keys():
alphabet_sequence = alphabet(letters=sequence["letters"])
else:
alphabet_sequence = alphabet(code=sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)

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

.......E..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

======================================================================
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 4.389s

FAILED (errors=2)

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

Христиан обнови решението на 31.03.2015 22:52 (преди над 9 години)

+from math import sqrt
+
+
+def fibonacci():
+ fib0 = 0
+ fib1 = 1
+ while True:
+ yield fib1
+ temp = fib0
+ fib0 = fib1
+ fib1 = temp + fib1
+
+
+def primes():
+ num = 2
+ # yield num
+ while True:
+ if num in [2, 3, 5, 7]:
+ yield num
+ else:
+ if num not in [4, 6, 8]:
+ limit = int(sqrt(num)) + 1
+ is_prime = all([num % index != 0 for index in range(2,
+ limit)])
+ if is_prime:
+ yield num
+ num += 1
+
+
+def alphabet(code="", letters=iter([])):
+ if code == "lat":
+ return (chr(index) for index in range(97, 123))
+ elif code == "bg":
+ return (chr(index) for index in range(1072, 1104) if index not in
+ [1101, 1099])
+ else:
+ return (letters[index] for index in range(len(letters)))
+
+
+def intertwined_sequences(sequences, generator_definitions={}):
+ fibonacci_sequence = []
+ primes_sequence = []
+ alphabet_sequence = []
+ extra_data = {}
+ has_parameter = False
+ for sequence in sequences:
+ if (generator_definitions != {} and
+ sequence["sequence"] in generator_definitions.keys()):
+ if len(sequence) >= 3:
+ temp = []
+ has_parameter = True
+ for item in sequence.keys():
+ if item != "sequence" and item != "length":
+ temp.append(sequence[item])
+ sequence_name = sequence["sequence"]
+ if extra_data.get(sequence_name) is None:
+ if not has_parameter:
+ extra_data[sequence_name] = \
+ iter(generator_definitions[sequence_name]())
+ else:
+ extra_data[sequence_name] = \
+ iter(generator_definitions[sequence_name](*temp))
+ for i in range(sequence["length"]):
+ yield next(extra_data[sequence_name])
+ if sequence["sequence"] == "fibonacci":
+ if fibonacci_sequence == []:
+ fibonacci_sequence = fibonacci()
+ for i in range(sequence["length"]):
+ yield next(fibonacci_sequence)
+ elif sequence["sequence"] == "primes":
+ if primes_sequence == []:
+ primes_sequence = primes()
+ for i in range(sequence["length"]):
+ yield next(primes_sequence)
+ elif sequence["sequence"] == "alphabet":
+ if alphabet_sequence == []:
+ alphabet_sequence = alphabet(sequence["code"])
+ for i in range(sequence["length"]):
+ yield next(alphabet_sequence)

Христиан обнови решението на 01.04.2015 10:57 (преди над 9 години)

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
# yield num
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
-def alphabet(code="", letters=iter([])):
- if code == "lat":
- return (chr(index) for index in range(97, 123))
- elif code == "bg":
- return (chr(index) for index in range(1072, 1104) if index not in
- [1101, 1099])
+def alphabet(code="", letters=[]):
+ if letters == []:
+ if code == "lat":
+ return (chr(index) for index in range(97, 123))
+ elif code == "bg":
+ return (chr(index) for index in range(1072, 1104) if index not in
+ [1101, 1099])
else:
- return (letters[index] for index in range(len(letters)))
+ return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
has_parameter = False
for sequence in sequences:
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
if alphabet_sequence == []:
alphabet_sequence = alphabet(sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)

Христиан обнови решението на 01.04.2015 10:58 (преди над 9 години)

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
- # yield num
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
def alphabet(code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
has_parameter = False
for sequence in sequences:
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
if alphabet_sequence == []:
alphabet_sequence = alphabet(sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)

Христиан обнови решението на 01.04.2015 11:41 (преди над 9 години)

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
def alphabet(code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
- has_parameter = False
for sequence in sequences:
+ has_parameter = False
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
if alphabet_sequence == []:
alphabet_sequence = alphabet(sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)

Христиан обнови решението на 02.04.2015 00:02 (преди над 9 години)

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
def alphabet(code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
for sequence in sequences:
has_parameter = False
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
if alphabet_sequence == []:
- alphabet_sequence = alphabet(sequence["code"])
+ if "code" not in sequence.keys():
+ alphabet_sequence = alphabet(sequence["letters"])
+ else:
+ alphabet_sequence = alphabet(sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)

Христиан обнови решението на 02.04.2015 23:30 (преди над 9 години)

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
-def alphabet(code="", letters=[]):
+def alphabet(*, code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
for sequence in sequences:
has_parameter = False
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
- if alphabet_sequence == []:
- if "code" not in sequence.keys():
- alphabet_sequence = alphabet(sequence["letters"])
- else:
- alphabet_sequence = alphabet(sequence["code"])
+ if "code" not in sequence.keys():
+ alphabet_sequence = alphabet(letters=sequence["letters"])
+ else:
+ alphabet_sequence = alphabet(code=sequence["code"])
for i in range(sequence["length"]):
- yield next(alphabet_sequence)
+ yield next(alphabet_sequence)

intertwined_sequences е ужасно сложно и най-вероятно объркано някъде. Тестовете timeout-ват. Имай предвид, че sequences може да бъде безкрайна редица и директно обхождане по този начин без да yield-ваш в блока на for-а е леко опасно.

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

from math import sqrt
def fibonacci():
fib0 = 0
fib1 = 1
while True:
yield fib1
temp = fib0
fib0 = fib1
fib1 = temp + fib1
def primes():
num = 2
while True:
if num in [2, 3, 5, 7]:
yield num
else:
if num not in [4, 6, 8]:
limit = int(sqrt(num)) + 1
is_prime = all([num % index != 0 for index in range(2,
limit)])
if is_prime:
yield num
num += 1
def alphabet(*, code="", letters=[]):
if letters == []:
if code == "lat":
return (chr(index) for index in range(97, 123))
elif code == "bg":
return (chr(index) for index in range(1072, 1104) if index not in
[1101, 1099])
else:
return iter(letters)
def intertwined_sequences(sequences, generator_definitions={}):
fibonacci_sequence = []
primes_sequence = []
alphabet_sequence = []
extra_data = {}
for sequence in sequences:
has_parameter = False
if (generator_definitions != {} and
sequence["sequence"] in generator_definitions.keys()):
if len(sequence) >= 3:
temp = []
has_parameter = True
for item in sequence.keys():
if item != "sequence" and item != "length":
temp.append(sequence[item])
sequence_name = sequence["sequence"]
if extra_data.get(sequence_name) is None:
if not has_parameter:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name]())
else:
extra_data[sequence_name] = \
iter(generator_definitions[sequence_name](*temp))
for i in range(sequence["length"]):
yield next(extra_data[sequence_name])
if sequence["sequence"] == "fibonacci":
if fibonacci_sequence == []:
fibonacci_sequence = fibonacci()
for i in range(sequence["length"]):
yield next(fibonacci_sequence)
elif sequence["sequence"] == "primes":
if primes_sequence == []:
primes_sequence = primes()
for i in range(sequence["length"]):
yield next(primes_sequence)
elif sequence["sequence"] == "alphabet":
- if "code" not in sequence.keys():
- alphabet_sequence = alphabet(letters=sequence["letters"])
- else:
- alphabet_sequence = alphabet(code=sequence["code"])
+ if alphabet_sequence == []:
+ if "code" not in sequence.keys():
+ alphabet_sequence = alphabet(letters=sequence["letters"])
+ else:
+ alphabet_sequence = alphabet(code=sequence["code"])
for i in range(sequence["length"]):
yield next(alphabet_sequence)