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

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

Към профила на Йоан Динков

Резултати

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

Код

def fibonacci():
current, next = 1, 1
while True:
yield current
current, next = next, current + next
def primes():
current = 1
while True:
if __is_prime(current):
yield current
current += 1
def alphabet(code='lat', letters=None):
types = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
if letters is None:
letters = types[code]
for letter in letters:
yield letter
def intertwined_sequences(iterators, generator_definitions={}):
sequences = {}
generic_params = ('sequence', 'length')
for iterator in iterators:
sequence = iterator['sequence']
params = {key: value for key, value
in iterator.items()
if key not in generic_params}
if sequence not in sequences:
if sequence in generator_definitions:
sequences[sequence] = generator_definitions[sequence](**params)
else:
sequences[sequence] = eval(sequence)(**params)
length = int(iterator['length'])
for i in range(0, length):
yield next(sequences[sequence])
def __is_prime(number):
"""http://en.wikipedia.org/wiki/Primality_test"""
if number <= 3:
return number >= 2
if number % 2 == 0 or number % 3 == 0:
return False
for i in range(5, int(number ** 0.5) + 1, 6):
if number % i == 0 or number % (i + 2) == 0:
return False
return True

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

......EE......
======================================================================
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

----------------------------------------------------------------------
Ran 14 tests in 4.239s

FAILED (errors=2)

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

Йоан обнови решението на 29.03.2015 22:24 (преди над 9 години)

+def fibonacci():
+ current, next = 1, 1
+
+ while True:
+ yield current
+ current, next = next, current + next
+
+
+def primes():
+ current = 1
+ found_primes = []
+ while True:
+ if __is_prime(current) and current not in found_primes:
+ found_primes.append(current)
+ yield current
+ else:
+ current += 1
+
+
+def alphabet(code='lat', letters=None):
+ types = {
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщьъюя'
+ }
+
+ input_alphabet = []
+
+ if (letters is not None):
+ input_letters = list(letters)
+ else:
+ input_letters = list(types[code])
+
+ for letter in input_letters:
+ yield letter
+
+
+def intertwined_sequences(iterators, generator_definitions={}):
+ sequences = {}
+ generic_params = ('sequence', 'length')
+
+ for iterator in iterators:
+ sequence = iterator['sequence']
+
+ params = {key: value for key, value
+ in iterator.items()
+ if key not in generic_params}
+
+ if sequence not in sequences:
+ if sequence in generator_definitions:
+ sequences[sequence] = generator_definitions[sequence](**params)
+ else:
+ sequences[sequence] = eval(sequence)(**params)
+
+ length = int(iterator['length'])
+
+ for i in range(0, length):
+ yield next(sequences[sequence])
+
+
+def __is_prime(number):
+ """http://en.wikipedia.org/wiki/Primality_test"""
+ if number <= 3:
+ return number >= 2
+ if number % 2 == 0 or number % 3 == 0:
+ return False
+ for i in range(5, int(number ** 0.5) + 1, 6):
+ if number % i == 0 or number % (i + 2) == 0:
+ return False
+ return True

Йоан обнови решението на 31.03.2015 13:55 (преди над 9 години)

def fibonacci():
current, next = 1, 1
while True:
yield current
current, next = next, current + next
def primes():
current = 1
- found_primes = []
+
while True:
- if __is_prime(current) and current not in found_primes:
- found_primes.append(current)
+ if __is_prime(current):
yield current
- else:
- current += 1
+ current += 1
+
def alphabet(code='lat', letters=None):
types = {
'lat': 'abcdefghijklmnopqrstuvwxyz',
- 'bg': 'абвгдежзийклмнопрстуфхцчшщьъюя'
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'
}
- input_alphabet = []
+ if letters is None:
+ letters = types[code]
- if (letters is not None):
- input_letters = list(letters)
- else:
- input_letters = list(types[code])
-
- for letter in input_letters:
+ for letter in letters:
yield letter
def intertwined_sequences(iterators, generator_definitions={}):
sequences = {}
generic_params = ('sequence', 'length')
for iterator in iterators:
sequence = iterator['sequence']
params = {key: value for key, value
in iterator.items()
if key not in generic_params}
if sequence not in sequences:
if sequence in generator_definitions:
sequences[sequence] = generator_definitions[sequence](**params)
else:
sequences[sequence] = eval(sequence)(**params)
length = int(iterator['length'])
for i in range(0, length):
yield next(sequences[sequence])
def __is_prime(number):
"""http://en.wikipedia.org/wiki/Primality_test"""
if number <= 3:
return number >= 2
if number % 2 == 0 or number % 3 == 0:
return False
for i in range(5, int(number ** 0.5) + 1, 6):
if number % i == 0 or number % (i + 2) == 0:
return False
return True