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

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

Към профила на Диана Генева

Резултати

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

Код

# encoding: utf-8
from itertools import count
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def is_prime(dividend):
if dividend < 2:
return False
if dividend == 2:
return True
for divisor in range(2, int(dividend ** 1 / 2) + 1):
if dividend % divisor == 0:
return False
return True
def primes():
return (number for number in count(1) if is_prime(number))
def alphabet(code=None, letters=None):
if code == 'lat':
alphabet = 'abcdefghijklmnopqrstuvwxyz'
else:
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters:
alphabet = letters
for letter in alphabet:
yield letter
def intertwined_sequences(generators, *, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet})
iterators = {}
for generator in generators:
arguments = {key: value for key, value in generator.items()
if key not in ('sequence', 'length')}
if generator['sequence'] not in iterators:
iter = generator_definitions[generator['sequence']](**arguments)
iterators[generator['sequence']] = iter
for _ in range(generator['length']):
yield next(iterators[generator['sequence']])

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

......E.......
======================================================================
ERROR: test_generator_definitions (test.TestIntertwine)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 2.836s

FAILED (errors=1)

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

Диана обнови решението на 29.03.2015 23:03 (преди над 9 години)

+# encoding: utf-8
+from itertools import count
+
+
+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def is_prime(dividend):
+ if dividend < 2:
+ return False
+ if dividend == 2:
+ return True
+ for divisor in range(2, int(dividend ** 1 / 2) + 1):
+ if dividend % divisor == 0:
+ return False
+ return True
+
+
+def primes():
+ return (number for number in count(1) if is_prime(number))
+
+
+def alphabet(code=None, letters=None):
+ if code == 'lat':
+ alphabet = 'abcdefghigklmnopqrstuvwxyz'
+ else:
+ alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ if letters:
+ alphabet = letters
+ for letter in alphabet:
+ yield letter
+
+
+def intertwined_sequences(generators, generator_definitions):
+ generator_definitions.update({'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet})
+ iterators = {}
+ for generator in generators:
+ arguments = {key: value for key, value in generator.items()
+ if key not in ('sequence', 'length')}
+ if generator['sequence'] not in iterators:
+ iter = generator_definitions[generator['sequence']](**arguments)
+ iterators[generator['sequence']] = iter
+ for _ in range(generator['length']):
+ yield next(iterators[generator['sequence']])

Диана обнови решението на 29.03.2015 23:08 (преди над 9 години)

# encoding: utf-8
from itertools import count
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def is_prime(dividend):
if dividend < 2:
return False
if dividend == 2:
return True
for divisor in range(2, int(dividend ** 1 / 2) + 1):
if dividend % divisor == 0:
return False
return True
def primes():
return (number for number in count(1) if is_prime(number))
def alphabet(code=None, letters=None):
if code == 'lat':
alphabet = 'abcdefghigklmnopqrstuvwxyz'
else:
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters:
alphabet = letters
for letter in alphabet:
yield letter
-def intertwined_sequences(generators, generator_definitions):
+def intertwined_sequences(generators, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet})
iterators = {}
for generator in generators:
arguments = {key: value for key, value in generator.items()
if key not in ('sequence', 'length')}
if generator['sequence'] not in iterators:
iter = generator_definitions[generator['sequence']](**arguments)
iterators[generator['sequence']] = iter
for _ in range(generator['length']):
yield next(iterators[generator['sequence']])

Диана обнови решението на 31.03.2015 23:00 (преди над 9 години)

# encoding: utf-8
from itertools import count
def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def is_prime(dividend):
if dividend < 2:
return False
if dividend == 2:
return True
for divisor in range(2, int(dividend ** 1 / 2) + 1):
if dividend % divisor == 0:
return False
return True
def primes():
return (number for number in count(1) if is_prime(number))
def alphabet(code=None, letters=None):
if code == 'lat':
- alphabet = 'abcdefghigklmnopqrstuvwxyz'
+ alphabet = 'abcdefghijklmnopqrstuvwxyz'
else:
alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
if letters:
alphabet = letters
for letter in alphabet:
yield letter
-def intertwined_sequences(generators, generator_definitions={}):
+def intertwined_sequences(generators, *, generator_definitions={}):
generator_definitions.update({'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet})
iterators = {}
for generator in generators:
arguments = {key: value for key, value in generator.items()
if key not in ('sequence', 'length')}
if generator['sequence'] not in iterators:
iter = generator_definitions[generator['sequence']](**arguments)
iterators[generator['sequence']] = iter
for _ in range(generator['length']):
yield next(iterators[generator['sequence']])