Решение на Генератори и итератори от Никола Илиев

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

Към профила на Никола Илиев

Резултати

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

Код

import math
def fibonacci():
current = 1
next = 1
while (True):
yield current
next += current
current = next - current
def primes():
current = 2
while (True):
if check_prime(current):
yield current
current += 1
def alphabet(*, code=None, letters=None):
if letters:
_alphabet = letters
elif code == 'lat':
_alphabet = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for index in range(len(_alphabet)):
yield _alphabet[index]
def intertwined_sequences(iterable, *, generator_definitions={}):
generator_definitions.update({
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
})
cache = {}
for generator_data in iterable:
sequence = generator_data.pop("sequence")
length = generator_data.pop("length")
if sequence not in cache:
cache[sequence] = iter(
generator_definitions[sequence](**generator_data))
generator = cache[sequence]
for index in range(length):
yield next(generator)
# Helper functions
def check_prime(number):
for i in range(2, int(math.sqrt(number)) + 1):
if number % i == 0:
return False
return True

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

.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.202s

FAILED (errors=1)

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

Никола обнови решението на 03.04.2015 00:59 (преди над 9 години)

+import math
+
+
+def fibonacci():
+ current = 1
+ next = 1
+ while (True):
+ yield current
+ next += current
+ current = next - current
+
+
+def primes():
+ current = 2
+ while (True):
+ if check_prime(current):
+ yield current
+ current += 1
+
+
+def alphabet(*, code=None, letters=None):
+ if letters:
+ _alphabet = letters
+ elif code == 'lat':
+ _alphabet = 'abcdefghijklmnopqrstuvwxyz'
+ elif code == 'bg':
+ _alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ else:
+ _alphabet = 'wtf'
+
+ for index in range(len(_alphabet)):
+ yield _alphabet[index]
+
+
+def intertwined_sequences(iterable, *, generator_definitions={}):
+ generator_definitions.update({
+ 'fibonacci': fibonacci,
+ 'primes': primes,
+ 'alphabet': alphabet
+ })
+ cache = {}
+ for generator_data in iterable:
+ sequence = generator_data.pop("sequence")
+ length = generator_data.pop("length")
+ if sequence not in cache:
+ cache[sequence] = iter(
+ generator_definitions[sequence](**generator_data))
+ generator = cache[sequence]
+ for index in range(length):
+ yield next(generator)
+
+
+# Helper functions
+
+
+def check_prime(number):
+ for i in range(2, int(math.sqrt(number)) + 1):
+ if number % i == 0:
+ return False
+ return True

Никола обнови решението на 03.04.2015 11:16 (преди над 9 години)

import math
def fibonacci():
current = 1
next = 1
while (True):
yield current
next += current
current = next - current
def primes():
current = 2
while (True):
if check_prime(current):
yield current
current += 1
def alphabet(*, code=None, letters=None):
if letters:
_alphabet = letters
elif code == 'lat':
_alphabet = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
_alphabet = 'абвгдежзийклмнопрстуфхцчшщъьюя'
- else:
- _alphabet = 'wtf'
for index in range(len(_alphabet)):
yield _alphabet[index]
def intertwined_sequences(iterable, *, generator_definitions={}):
generator_definitions.update({
'fibonacci': fibonacci,
'primes': primes,
'alphabet': alphabet
})
cache = {}
for generator_data in iterable:
sequence = generator_data.pop("sequence")
length = generator_data.pop("length")
if sequence not in cache:
cache[sequence] = iter(
generator_definitions[sequence](**generator_data))
generator = cache[sequence]
for index in range(length):
yield next(generator)
# Helper functions
-
-
def check_prime(number):
for i in range(2, int(math.sqrt(number)) + 1):
if number % i == 0:
return False
return True