Решение на Генератори и итератори от Цветан Мадански

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

Към профила на Цветан Мадански

Резултати

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

Код

lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z']
bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
def fibonacci_of(number):
if number in [0, 1]:
return 1
return fibonacci_of(number - 1) + fibonacci_of(number - 2)
def fibonacci():
counter = 0
while True:
yield fibonacci_of(counter)
counter += 1
def ones():
while True:
yield 1
def naturals():
number = 1
while True:
yield number
number += 1
def multiples_of(num):
i = 1
while True:
yield num * i
i += 1
def isPrime(number):
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
def primes():
number = 2
while True:
if isPrime(number):
yield number
number += 1
def alphabet(code='', letters=lat_alphabet):
if letters != lat_alphabet:
for letter in letters:
yield letter
else:
if code == 'bg':
letters = bg_alphabet
for letter in letters:
yield letter
def intertwined_sequences(iterable, generator_definitions={}):
fibonacci_gen = fibonacci()
primes_gen = primes()
defintions_helper = {}
arguments = {}
alphabet_help = {}
for elements in iterable:
if elements['sequence'] in defintions_helper.keys():
if type(defintions_helper[elements['sequence']]) \
is not type(fibonacci()):
gen_of_element = iter(defintions_helper[elements['sequence']])
else:
gen_of_element = defintions_helper[elements['sequence']]
for _ in range(elements['length']):
yield next(gen_of_element)
elif elements['sequence'] in generator_definitions.keys():
if len(elements) > 2:
for x in list(elements.items()):
if x[0] not in ['sequence', 'length']:
arguments[x[0]] = x[1]
gen_starter = \
generator_definitions[elements['sequence']](**arguments)
else:
gen_starter = generator_definitions[elements['sequence']]
if type(gen_starter) is not type(fibonacci()):
if type(gen_starter()) is range:
defintions_helper[elements['sequence']] = \
iter(gen_starter())
else:
defintions_helper[elements['sequence']] = gen_starter()
else:
defintions_helper[elements['sequence']] = gen_starter
if type(defintions_helper[elements['sequence']]) \
is not type(fibonacci()):
gen_of_element = iter(defintions_helper[elements['sequence']])
else:
gen_of_element = defintions_helper[elements['sequence']]
for _ in range(elements['length']):
yield next(gen_of_element)
elif elements['sequence'] is 'fibonacci':
for _ in range(elements['length']):
yield next(fibonacci_gen)
elif elements['sequence'] is 'primes':
for _ in range(elements['length']):
yield next(primes_gen)
elif elements['sequence'] is 'alphabet':
if elements['sequence'] in alphabet_help.values():
for _ in range(elements['length']):
yield next(alphab)
else:
alphabet_help['sequence'] = 'alphabet'
if 'letters' in elements.keys():
alphab = alphabet(letters=elements['letters'])
for _ in range(elements['length']):
yield next(alphab)
elif elements['code'] is 'lat':
alphab = iter(lat_alphabet)
for _ in range(elements['length']):
yield next(alphab)
else:
alphab = iter(bg_alphabet)
for _ in range(elements['length']):
yield next(alphab)

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

.....E.E......
======================================================================
ERROR: test_longer_sequence (test.TestFibonacci)
----------------------------------------------------------------------
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.281s

FAILED (errors=2)

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

Цветан обнови решението на 03.04.2015 12:04 (преди над 9 години)

+lat_alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+ 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
+ 'u', 'v', 'w', 'x', 'y', 'z']
+
+
+bg_alphabet = ['а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
+ 'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х',
+ 'ц', 'ч', 'ш', 'щ', 'ъ', 'ь', 'ю', 'я']
+
+
+def fibonacci_of(number):
+ if number in [0, 1]:
+ return 1
+ return fibonacci_of(number - 1) + fibonacci_of(number - 2)
+
+
+def fibonacci():
+ counter = 0
+ while True:
+ yield fibonacci_of(counter)
+ counter += 1
+
+
+def ones():
+ while True:
+ yield 1
+
+
+def naturals():
+ number = 1
+ while True:
+ yield number
+ number += 1
+
+
+def multiples_of(num):
+ i = 1
+ while True:
+ yield num * i
+ i += 1
+
+
+def isPrime(number):
+ for i in range(2, int(number ** 0.5) + 1):
+ if number % i == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if isPrime(number):
+ yield number
+ number += 1
+
+
+def alphabet(code='', letters=lat_alphabet):
+ if letters != lat_alphabet:
+ for letter in letters:
+ yield letter
+ else:
+ if code == 'bg':
+ letters = bg_alphabet
+ for letter in letters:
+ yield letter
+
+
+def intertwined_sequences(iterable, generator_definitions={}):
+ fibonacci_gen = fibonacci()
+ primes_gen = primes()
+ defintions_helper = {}
+ arguments = {}
+ alphabet_help = {}
+ for elements in iterable:
+ if elements['sequence'] in defintions_helper.keys():
+ if type(defintions_helper[elements['sequence']]) \
+ is not type(fibonacci()):
+ gen_of_element = iter(defintions_helper[elements['sequence']])
+ else:
+ gen_of_element = defintions_helper[elements['sequence']]
+ for _ in range(elements['length']):
+ yield next(gen_of_element)
+
+ elif elements['sequence'] in generator_definitions.keys():
+ if len(elements) > 2:
+ for x in list(elements.items()):
+ if x[0] not in ['sequence', 'length']:
+ arguments[x[0]] = x[1]
+ gen_starter = \
+ generator_definitions[elements['sequence']](**arguments)
+ else:
+ gen_starter = generator_definitions[elements['sequence']]
+
+ if type(gen_starter) is not type(fibonacci()):
+ if type(gen_starter()) is range:
+ defintions_helper[elements['sequence']] = \
+ iter(gen_starter())
+ else:
+ defintions_helper[elements['sequence']] = gen_starter()
+ else:
+ defintions_helper[elements['sequence']] = gen_starter
+
+ if type(defintions_helper[elements['sequence']]) \
+ is not type(fibonacci()):
+ gen_of_element = iter(defintions_helper[elements['sequence']])
+ else:
+ gen_of_element = defintions_helper[elements['sequence']]
+
+ for _ in range(elements['length']):
+ yield next(gen_of_element)
+
+ elif elements['sequence'] is 'fibonacci':
+ for _ in range(elements['length']):
+ yield next(fibonacci_gen)
+
+ elif elements['sequence'] is 'primes':
+ for _ in range(elements['length']):
+ yield next(primes_gen)
+
+ elif elements['sequence'] is 'alphabet':
+ if elements['sequence'] in alphabet_help.values():
+ for _ in range(elements['length']):
+ yield next(alphab)
+
+ else:
+ alphabet_help['sequence'] = 'alphabet'
+ if 'letters' in elements.keys():
+ alphab = alphabet(letters=elements['letters'])
+ for _ in range(elements['length']):
+ yield next(alphab)
+
+ elif elements['code'] is 'lat':
+ alphab = iter(lat_alphabet)
+ for _ in range(elements['length']):
+ yield next(alphab)
+
+ else:
+ alphab = iter(bg_alphabet)
+ for _ in range(elements['length']):
+ yield next(alphab)