Решение на Генератори и итератори от Гюлджан Купен

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

Към профила на Гюлджан Купен

Резултати

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

Код

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
number = 2
while True:
is_prime = True
for x in range(2, number):
if number % x == 0:
is_prime = False
break
if is_prime:
yield number
number += 1
def alphabet(*, code='lat', letters=[]):
if letters:
iterable = letters
else:
if code == 'lat':
iterable = list('abcdefghijklmnopqrstuvwxyz')
else:
iterable = list('абвгдежзийклмнопрстуфхцчшщъьюя')
for letter in iterable:
yield letter
def to_generator(name, code_='', letters_=[]):
if name == 'fibonacci':
return fibonacci()
elif name == 'primes':
return primes()
elif name == 'alphabet':
if letters_:
return alphabet(code=code_, letters=letters_)
else:
return alphabet(code=code_)
def intertwined_sequences(items, *, generator_definitions={}):
generators = {}
for item in items:
sequence = item['sequence']
if item['sequence'] not in generators:
if item['sequence'] == 'alphabet':
if 'code' not in item:
generators[sequence] = to_generator('alphabet',
item['letters'])
elif 'letters' not in item:
generators[sequence] = to_generator('alphabet',
item['code'])
else:
generators[sequence] = to_generator('alphabet',
item['code'],
item['letters'])
elif sequence == 'fibonacci' or sequence == 'primes':
generators[sequence] = to_generator(sequence)
else:
args = [value for key, value in item.items()
if key != 'sequence' and key != 'length']
generators[sequence] = iter(generator_definitions[
sequence](*args))
length = item['length']
while length > 0:
yield next(generators[sequence])
length -= 1

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

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

----------------------------------------------------------------------
Ran 14 tests in 3.558s

FAILED (errors=1)

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

Гюлджан обнови решението на 01.04.2015 00:59 (преди над 9 години)

+def fibonacci():
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+def primes():
+ number = 2
+ while True:
+ is_prime = True
+ for x in range(2, number):
+ if number % x == 0:
+ is_prime = False
+ break
+ if is_prime:
+ yield number
+ number += 1
+
+
+def alphabet(*, code='lat', letters=[]):
+ if letters:
+ iterable = letters
+ else:
+ if code == 'lat':
+ iterable = list('abcdefghijklmnopqrstuvwxyz')
+ else:
+ iterable = list('абвгдежзийклмнопрстуфхцчщъьююя')
+ for letter in iterable:
+ yield letter
+
+
+def to_generator(string, code_='', letters_=[]):
+ if string == 'fibonacci':
+ return fibonacci()
+ elif string == 'primes':
+ return primes()
+ elif string == 'alphabet':
+ if letters_:
+ return alphabet(code=code_, letters=letters_)
+ else:
+ return alphabet(code=code_)
+
+
+def intertwined_sequences(items, *, generator_definitions={}):
+ generators = {}
+ for item in items:
+ sequence = item['sequence']
+ if item['sequence'] not in generators:
+ if item['sequence'] == 'alphabet':
+ if 'code' not in item:
+ generators[sequence] = to_generator('alphabet',
+ item['letters'])
+ elif 'letters' not in item:
+ generators[sequence] = to_generator('alphabet',
+ item['code'])
+ else:
+ generators[sequence] = to_generator('alphabet',
+ item['code'],
+ item['letters'])
+ elif sequence == 'fibonacci' or sequence == 'primes':
+ generators[sequence] = to_generator(sequence)
+ else:
+ generators[sequence] = iter(generator_definitions[sequence]())
+ length = item['length']
+ while length > 0:
+ yield next(generators[sequence])
+ length -= 1

Гюлджан обнови решението на 01.04.2015 11:09 (преди над 9 години)

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
number = 2
while True:
is_prime = True
for x in range(2, number):
if number % x == 0:
is_prime = False
break
if is_prime:
yield number
number += 1
def alphabet(*, code='lat', letters=[]):
if letters:
iterable = letters
else:
if code == 'lat':
iterable = list('abcdefghijklmnopqrstuvwxyz')
else:
iterable = list('абвгдежзийклмнопрстуфхцчщъьююя')
for letter in iterable:
yield letter
def to_generator(string, code_='', letters_=[]):
if string == 'fibonacci':
return fibonacci()
elif string == 'primes':
return primes()
elif string == 'alphabet':
if letters_:
return alphabet(code=code_, letters=letters_)
else:
return alphabet(code=code_)
def intertwined_sequences(items, *, generator_definitions={}):
generators = {}
for item in items:
sequence = item['sequence']
if item['sequence'] not in generators:
if item['sequence'] == 'alphabet':
if 'code' not in item:
generators[sequence] = to_generator('alphabet',
item['letters'])
elif 'letters' not in item:
generators[sequence] = to_generator('alphabet',
item['code'])
else:
generators[sequence] = to_generator('alphabet',
item['code'],
item['letters'])
elif sequence == 'fibonacci' or sequence == 'primes':
generators[sequence] = to_generator(sequence)
else:
- generators[sequence] = iter(generator_definitions[sequence]())
+ args = [y for x, y in item.items()
+ if x != 'sequence' and x != 'length']
+ generators[sequence] = iter(generator_definitions[
+ sequence](*args))
length = item['length']
while length > 0:
yield next(generators[sequence])
length -= 1

Гюлджан обнови решението на 01.04.2015 23:35 (преди над 9 години)

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
def primes():
number = 2
while True:
is_prime = True
for x in range(2, number):
if number % x == 0:
is_prime = False
break
if is_prime:
yield number
number += 1
def alphabet(*, code='lat', letters=[]):
if letters:
iterable = letters
else:
if code == 'lat':
iterable = list('abcdefghijklmnopqrstuvwxyz')
else:
- iterable = list('абвгдежзийклмнопрстуфхцчщъьююя')
+ iterable = list('абвгдежзийклмнопрстуфхцчшщъьюя')
for letter in iterable:
yield letter
-def to_generator(string, code_='', letters_=[]):
- if string == 'fibonacci':
+def to_generator(name, code_='', letters_=[]):
+ if name == 'fibonacci':
return fibonacci()
- elif string == 'primes':
+ elif name == 'primes':
return primes()
- elif string == 'alphabet':
+ elif name == 'alphabet':
if letters_:
return alphabet(code=code_, letters=letters_)
else:
return alphabet(code=code_)
def intertwined_sequences(items, *, generator_definitions={}):
generators = {}
for item in items:
sequence = item['sequence']
if item['sequence'] not in generators:
if item['sequence'] == 'alphabet':
if 'code' not in item:
generators[sequence] = to_generator('alphabet',
item['letters'])
elif 'letters' not in item:
generators[sequence] = to_generator('alphabet',
item['code'])
else:
generators[sequence] = to_generator('alphabet',
item['code'],
item['letters'])
elif sequence == 'fibonacci' or sequence == 'primes':
generators[sequence] = to_generator(sequence)
else:
- args = [y for x, y in item.items()
- if x != 'sequence' and x != 'length']
+ args = [value for key, value in item.items()
+ if key != 'sequence' and key != 'length']
generators[sequence] = iter(generator_definitions[
sequence](*args))
length = item['length']
while length > 0:
yield next(generators[sequence])
length -= 1