Решение на Генератори и итератори от Михаел Димитров

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

Към профила на Михаел Димитров

Резултати

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

Код

from itertools import count
import collections
import string
def intertwined_sequences(orders, generator_definitions={}):
generators = {}
for order in orders:
sequence_name = order['sequence']
length = order['length']
del order['length']
del order['sequence']
if sequence_name not in generators:
definition = globals().get(sequence_name)
if not definition:
definition = generator_definitions[sequence_name]
generators[sequence_name] = definition(**order)
if isinstance(generators[sequence_name], collections.Iterator) is not True:
generators[sequence_name] = (x for x in generators[sequence_name])
for iterations in range(0, length):
yield next(generators[sequence_name])
def fibonacci():
prev_value = 0
next_value = 1
while True:
# 1 is the first fib number according the tests provided
yield next_value
new_value = prev_value + next_value
prev_value = next_value
next_value = new_value
def primes():
""" better solution here:
http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/
"""
return (x for x in count() if is_prime(x))
def is_prime(n):
""" naive primality test """
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
letters = kwargs.get('letters', [])
if not letters:
return (c for c in get_alphabet(kwargs['code']))
else:
return (c.lower() for c in list(letters))
def get_alphabet(code):
codes = {
'bg': [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
],
'lat': list(string.ascii_lowercase)
}
return codes.get(code, [])
def ones():
while True:
yield 1
def naturals():
number = 1
while True:
yield number
number += 1

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

.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

----------------------------------------------------------------------
Ran 14 tests in 2.322s

FAILED (errors=1)

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

Михаел обнови решението на 31.03.2015 01:12 (преди над 9 години)

+from itertools import count
+import string
+
+
+def intertwined_sequences(orders, generator_definitions={}):
+
+ generators = {}
+ for order in orders:
+ sequence_name = order['sequence']
+ length = order['length']
+ del order['length']
+ del order['sequence']
+
+ if sequence_name not in generators:
+ definition = globals().get(sequence_name)
+ if not definition:
+ definition = generator_definitions[sequence_name]
+ generators[sequence_name] = definition(**order)
+
+ for iterations in range(0, length):
+ yield next(generators[sequence_name])
+
+
+def fibonacci():
+ prev_value = 0
+ next_value = 1
+ while True:
+ # 1 is the first fib number according the tests provided
+ yield next_value
+ new_value = prev_value + next_value
+ prev_value = next_value
+ next_value = new_value
+
+
+def primes():
+ """ better solution here:
+ http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/
+ """
+ return (x for x in count() if is_prime(x))
+
+
+def is_prime(n):
+ """ naive primality test """
+
+ if n <= 3:
+ return n >= 2
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+
+def alphabet(**kwargs):
+ letters = kwargs.setdefault('letters', [])
+ if not letters:
+ return (c for c in get_alphabet(kwargs['code']))
+ else:
+ return (c.lower() for c in list(letters))
+
+
+def get_alphabet(code):
+ codes = {
+ 'bg': [
+ 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
+ 'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
+ 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
+ 'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
+ ],
+ 'lat': list(string.ascii_lowercase)
+ }
+
+ return codes.setdefault(code, [])

Михаел обнови решението на 31.03.2015 10:24 (преди над 9 години)

from itertools import count
import string
def intertwined_sequences(orders, generator_definitions={}):
-
generators = {}
for order in orders:
sequence_name = order['sequence']
length = order['length']
del order['length']
del order['sequence']
if sequence_name not in generators:
definition = globals().get(sequence_name)
if not definition:
definition = generator_definitions[sequence_name]
generators[sequence_name] = definition(**order)
for iterations in range(0, length):
yield next(generators[sequence_name])
def fibonacci():
prev_value = 0
next_value = 1
while True:
# 1 is the first fib number according the tests provided
yield next_value
new_value = prev_value + next_value
prev_value = next_value
next_value = new_value
def primes():
""" better solution here:
http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/
"""
return (x for x in count() if is_prime(x))
def is_prime(n):
""" naive primality test """
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
letters = kwargs.setdefault('letters', [])
if not letters:
return (c for c in get_alphabet(kwargs['code']))
else:
return (c.lower() for c in list(letters))
def get_alphabet(code):
codes = {
'bg': [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
],
'lat': list(string.ascii_lowercase)
}
return codes.setdefault(code, [])

Михаел обнови решението на 03.04.2015 14:59 (преди над 9 години)

from itertools import count
+import collections
import string
def intertwined_sequences(orders, generator_definitions={}):
generators = {}
for order in orders:
sequence_name = order['sequence']
length = order['length']
del order['length']
del order['sequence']
if sequence_name not in generators:
definition = globals().get(sequence_name)
+
if not definition:
definition = generator_definitions[sequence_name]
generators[sequence_name] = definition(**order)
+ if isinstance(generators[sequence_name], collections.Iterator) is not True:
+ generators[sequence_name] = (x for x in generators[sequence_name])
+
for iterations in range(0, length):
yield next(generators[sequence_name])
def fibonacci():
prev_value = 0
next_value = 1
while True:
# 1 is the first fib number according the tests provided
yield next_value
new_value = prev_value + next_value
prev_value = next_value
next_value = new_value
def primes():
""" better solution here:
http://code.activestate.com/recipes/117119-sieve-of-eratosthenes/
"""
return (x for x in count() if is_prime(x))
def is_prime(n):
""" naive primality test """
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
def alphabet(**kwargs):
- letters = kwargs.setdefault('letters', [])
+ letters = kwargs.get('letters', [])
if not letters:
return (c for c in get_alphabet(kwargs['code']))
else:
return (c.lower() for c in list(letters))
def get_alphabet(code):
codes = {
'bg': [
'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з',
'и', 'й', 'к', 'л', 'м', 'н', 'о', 'п',
'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч',
'ш', 'щ', 'ъ', 'ь', 'ю', 'я'
],
'lat': list(string.ascii_lowercase)
}
- return codes.setdefault(code, [])
+ return codes.get(code, [])
+
+
+def ones():
+ while True:
+ yield 1
+
+
+def naturals():
+ number = 1
+ while True:
+ yield number
+ number += 1