Решение на Генератори и итератори от Любослава Димитрова

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

Към профила на Любослава Димитрова

Резултати

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

Код

import itertools
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
# Sieve of Eratosthenes algorithm, using a dictionary
def primes():
dictionary = {}
for number in itertools.count(2):
last = dictionary.pop(number, None)
if last is None:
yield number
dictionary[number*number] = number
else:
something = last + number
while something in dictionary:
something += last
dictionary[something] = last
alphabets = {
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
"lat": "abcdefghijklmnopqrstuvwxyz"
}
def alphabet(code=None, letters=None):
if code in alphabets:
for item in alphabets[code]:
yield item
else:
for item in letters:
yield item
generators = {
"fibonacci": fibonacci(),
"primes": primes(),
"alphabet": alphabet
}
def intertwined_sequences(iterable, generator_definitions=None):
generator = None
if generator_definitions is not None:
for key in generator_definitions:
generators[key] = generator_definitions[key]
for item in iterable:
for key in item:
if key not in ("sequence", "length"):
kw = {}
kw[key] = item[key]
generator = generators[item["sequence"]](**kw)
generator = generators[item["sequence"]]
for _ in range(item["length"]):
yield next(generator)

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

......EEEEEE..
======================================================================
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_kwargs_generator (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

----------------------------------------------------------------------
Ran 14 tests in 12.160s

FAILED (errors=6)

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

Любослава обнови решението на 03.04.2015 13:12 (преди над 9 години)

+import itertools
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+# Sieve of Eratosthenes algorithm, using a dictionary
+def primes():
+ dictionary = {}
+ for number in itertools.count(2):
+ last = dictionary.pop(number, None)
+ if last is None:
+ yield number
+ dictionary[number*number] = number
+ else:
+ something = last + number
+ while something in dictionary:
+ something += last
+ dictionary[something] = last
+

Любослава обнови решението на 03.04.2015 14:03 (преди над 9 години)

import itertools
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
# Sieve of Eratosthenes algorithm, using a dictionary
def primes():
dictionary = {}
for number in itertools.count(2):
last = dictionary.pop(number, None)
if last is None:
yield number
dictionary[number*number] = number
else:
something = last + number
while something in dictionary:
something += last
dictionary[something] = last
+
+alphabets = {
+ "bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
+ "lat": "abcdefghijklmnopqrstuvwxyz"
+ }
+
+
+def alphabet(code=None, letters=None):
+ if code in alphabets:
+ for item in alphabets[code]:
+ yield item
+ else:
+ for item in letters:
+ yield item

Любослава обнови решението на 03.04.2015 15:35 (преди над 9 години)

import itertools
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
# Sieve of Eratosthenes algorithm, using a dictionary
def primes():
dictionary = {}
for number in itertools.count(2):
last = dictionary.pop(number, None)
if last is None:
yield number
dictionary[number*number] = number
else:
something = last + number
while something in dictionary:
something += last
dictionary[something] = last
alphabets = {
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
"lat": "abcdefghijklmnopqrstuvwxyz"
}
def alphabet(code=None, letters=None):
if code in alphabets:
for item in alphabets[code]:
yield item
else:
for item in letters:
- yield item
+ yield item
+
+
+generators = {
+ "fibonacci": fibonacci(),
+ "primes": primes(),
+ "alphabet": alphabet
+ }
+
+
+def intertwined_sequences(iterable):
+ generator = None
+ for item in iterable:
+ if "code" in item:
+ generator = generators[item["sequence"]](code=item["code"])
+ elif "letters" in item:
+ generator = generators[item["sequence"]](letters=item["letters"])
+ else:
+ generator = generators[item["sequence"]]
+ for _ in range(item["length"]):
+ yield next(generator)

Любослава обнови решението на 03.04.2015 16:38 (преди над 9 години)

import itertools
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
# Sieve of Eratosthenes algorithm, using a dictionary
def primes():
dictionary = {}
for number in itertools.count(2):
last = dictionary.pop(number, None)
if last is None:
yield number
dictionary[number*number] = number
else:
something = last + number
while something in dictionary:
something += last
dictionary[something] = last
alphabets = {
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
"lat": "abcdefghijklmnopqrstuvwxyz"
}
def alphabet(code=None, letters=None):
if code in alphabets:
for item in alphabets[code]:
yield item
else:
for item in letters:
yield item
generators = {
"fibonacci": fibonacci(),
"primes": primes(),
"alphabet": alphabet
}
-def intertwined_sequences(iterable):
+def intertwined_sequences(iterable, generator_definitions=None):
generator = None
+ if generator_definitions is not None:
+ for key in generator_definitions:
+ generators[key] = generator_definitions[key]
for item in iterable:
- if "code" in item:
- generator = generators[item["sequence"]](code=item["code"])
- elif "letters" in item:
- generator = generators[item["sequence"]](letters=item["letters"])
- else:
- generator = generators[item["sequence"]]
+ for key in item:
+ if key not in ("sequence", "length"):
+ kw = {}
+ kw[key] = item[key]
+ generator = generators[item["sequence"]](**kw)
+ else:
+ generator = generators[item["sequence"]]
for _ in range(item["length"]):
- yield next(generator)
+ yield next(generator)

Любослава обнови решението на 03.04.2015 16:59 (преди над 9 години)

import itertools
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
# Sieve of Eratosthenes algorithm, using a dictionary
def primes():
dictionary = {}
for number in itertools.count(2):
last = dictionary.pop(number, None)
if last is None:
yield number
dictionary[number*number] = number
else:
something = last + number
while something in dictionary:
something += last
dictionary[something] = last
alphabets = {
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя",
"lat": "abcdefghijklmnopqrstuvwxyz"
}
def alphabet(code=None, letters=None):
if code in alphabets:
for item in alphabets[code]:
yield item
else:
for item in letters:
yield item
generators = {
"fibonacci": fibonacci(),
"primes": primes(),
"alphabet": alphabet
}
def intertwined_sequences(iterable, generator_definitions=None):
generator = None
if generator_definitions is not None:
for key in generator_definitions:
generators[key] = generator_definitions[key]
for item in iterable:
for key in item:
if key not in ("sequence", "length"):
kw = {}
kw[key] = item[key]
generator = generators[item["sequence"]](**kw)
- else:
- generator = generators[item["sequence"]]
+ generator = generators[item["sequence"]]
for _ in range(item["length"]):
- yield next(generator)
+ yield next(generator)