Решение на Генератори и итератори от Александър Наджарян

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

Към профила на Александър Наджарян

Резултати

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

Код

from math import sqrt
def fibonacci():
fib_1, fib_2 = 0, 1
while True:
fib_1, fib_2 = fib_2, fib_1 + fib_2
yield fib_1
def is_prime(num):
if num == 2 or num == 3:
return True
for divisor in range(2, int(sqrt(num))+1):
if num % divisor == 0:
return False
return True
def primes():
number = 1
while True:
number += 1
if(is_prime(number)):
yield number
LETTERS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghigklmnopqrstuvwxyz'
}
def alphabet(code=None, letters=None):
if code in LETTERS and not letters:
letters = LETTERS[code]
for letter_index in range(len(letters)):
yield letters[letter_index]
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def create(generator, generator_definitions):
name = generator['sequence']
kwargs = {}
for key in generator:
if key not in ('sequence', 'length'):
kwargs[key] = generator[key]
if name in GENERATORS:
new_generator = GENERATORS[name](**kwargs)
else:
new_generator = generator_definitions[name](**kwargs)
if not hasattr(new_generator, '__next__'):
new_generator = iter(new_generator)
return new_generator
def intertwined_sequences(sequence, generator_definitions=None):
generators = {}
for generator in sequence:
name = generator['sequence']
if name not in generators:
generators[name] = create(generator, generator_definitions)
for _ in range(generator['length']):
yield next(generators[name])

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

.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

----------------------------------------------------------------------
Ran 14 tests in 4.209s

FAILED (errors=2)

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

Александър обнови решението на 03.04.2015 13:10 (преди над 9 години)

+from math import sqrt
+
+
+def fibonacci():
+ fib_1, fib_2 = 0, 1
+ while True:
+ fib_1, fib_2 = fib_2, fib_1 + fib_2
+ yield fib_1
+
+
+def is_prime(num):
+ if num == 2 or num == 3:
+ return True
+
+ for divisor in range(2, int(sqrt(num))+1):
+ if num % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 1
+ while True:
+ number += 1
+ if(is_prime(number)):
+ yield number
+
+
+LETTERS = {
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': 'abcdefghigklmnopqrstuvwxyz'
+ }
+
+
+def alphabet(code=None, letters=None):
+ if code in LETTERS and not letters:
+ letters = LETTERS[code]
+ for letter_index in range(len(letters)):
+ yield letters[letter_index]
+
+
+GENERATORS = {
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ }
+
+
+def create(generator, generator_definitions):
+ name = generator['sequence']
+ kwargs = {}
+ for key in generator:
+ if key not in ('sequence', 'length'):
+ kwargs[key] = generator[key]
+ if name in GENERATORS:
+ new_generator = GENERATORS[name](**kwargs)
+ else:
+ new_generator = generator_definitions[name](**kwargs)
+
+ return new_generator
+
+
+def intertwined_sequences(sequence, generator_definitions=None):
+ generators = {}
+ for generator in sequence:
+ name = generator['sequence']
+ if name not in generators:
+ generators[name] = create(generator, generator_definitions)
+
+ for _ in range(generator['length']):
+ yield next(generators[name])

Александър обнови решението на 03.04.2015 13:21 (преди над 9 години)

from math import sqrt
+from math import sqrt
def fibonacci():
fib_1, fib_2 = 0, 1
while True:
fib_1, fib_2 = fib_2, fib_1 + fib_2
yield fib_1
def is_prime(num):
if num == 2 or num == 3:
return True
for divisor in range(2, int(sqrt(num))+1):
if num % divisor == 0:
return False
return True
def primes():
number = 1
while True:
number += 1
if(is_prime(number)):
yield number
LETTERS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghigklmnopqrstuvwxyz'
}
def alphabet(code=None, letters=None):
if code in LETTERS and not letters:
letters = LETTERS[code]
for letter_index in range(len(letters)):
yield letters[letter_index]
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def create(generator, generator_definitions):
name = generator['sequence']
kwargs = {}
for key in generator:
if key not in ('sequence', 'length'):
kwargs[key] = generator[key]
if name in GENERATORS:
new_generator = GENERATORS[name](**kwargs)
else:
new_generator = generator_definitions[name](**kwargs)
+
+ if not hasattr(new_generator, '__next__'):
+ new_generator = iter(new_generator)
return new_generator
def intertwined_sequences(sequence, generator_definitions=None):
generators = {}
for generator in sequence:
name = generator['sequence']
if name not in generators:
generators[name] = create(generator, generator_definitions)
for _ in range(generator['length']):
yield next(generators[name])

Александър обнови решението на 03.04.2015 13:23 (преди над 9 години)

from math import sqrt
-from math import sqrt
def fibonacci():
fib_1, fib_2 = 0, 1
while True:
fib_1, fib_2 = fib_2, fib_1 + fib_2
yield fib_1
def is_prime(num):
if num == 2 or num == 3:
return True
for divisor in range(2, int(sqrt(num))+1):
if num % divisor == 0:
return False
return True
def primes():
number = 1
while True:
number += 1
if(is_prime(number)):
yield number
LETTERS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghigklmnopqrstuvwxyz'
}
def alphabet(code=None, letters=None):
if code in LETTERS and not letters:
letters = LETTERS[code]
for letter_index in range(len(letters)):
yield letters[letter_index]
GENERATORS = {
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
}
def create(generator, generator_definitions):
name = generator['sequence']
kwargs = {}
for key in generator:
if key not in ('sequence', 'length'):
kwargs[key] = generator[key]
if name in GENERATORS:
new_generator = GENERATORS[name](**kwargs)
else:
new_generator = generator_definitions[name](**kwargs)
if not hasattr(new_generator, '__next__'):
new_generator = iter(new_generator)
return new_generator
def intertwined_sequences(sequence, generator_definitions=None):
generators = {}
for generator in sequence:
name = generator['sequence']
if name not in generators:
generators[name] = create(generator, generator_definitions)
for _ in range(generator['length']):
yield next(generators[name])