Решение на Генератори и итератори от Иван Тодоров

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

Към профила на Иван Тодоров

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 8 успешни тест(а)
  • 6 неуспешни тест(а)

Код

def fibonacci():
first = 1
second = 1
while True:
yield second
x = second
second = first
first += x
def primes():
prime_number = 2
flag = 0
while True:
for i in range(2, prime_number):
if prime_number % i == 0:
flag = 1
break
else:
pass
if flag == 0:
yield prime_number
else:
flag = 0
prime_number += 1
def alphabet(code='', letters=''):
def alphabet_help(letters):
for i in range(0, len(letters)):
yield letters[i]
while True:
if letters != '':
return alphabet_help(letters)
if code == 'bg':
return alphabet_help("абвгдежзийклмнопрстуфхцчшщъьюя")
elif code == 'lat':
return alphabet_help("abcdefghijklmnopqrstuvwxyz")
def intertwined_sequences(iter_object):
work_list = []
fibon = fibonacci()
prime = primes()
for i in range(0, len(iter_object)):
if iter_object.__getitem__(i)['sequence'] == 'fibonacci':
for j in range(0, iter_object.__getitem__(i)['length']):
work_list.append(next(fibon))
elif iter_object.__getitem__(i)['sequence'] == 'primes':
for j in range(0, iter_object.__getitem__(i)['length']):
work_list.append(next(prime))
elif iter_object.__getitem__(i)['sequence'] == 'alphabet':
if 'letters' in iter_object.__getitem__(i):
alpha = alphabet(letters=iter_object.__getitem__(i)['letters'])
for j in range(0, iter_object.__getitem__(i)['length']):
work_list.append(next(alpha))
else:
alpha_second = alphabet(code=iter_object.__getitem__(i)['code'])
for j in range(0, iter_object.__getitem__(i)['length']):
work_list.append(next(alpha_second))
return work_list # nai-groznoto parche kod , koeto sym pisal ...no raboti

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

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

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

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 13.514s

FAILED (errors=6)

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

Иван обнови решението на 03.04.2015 15:36 (преди над 9 години)

+def fibonacci():
+ first = 1
+ second = 1
+ while True:
+ yield second
+ x = second
+ second = first
+ first += x
+
+
+def primes():
+ prime_number = 2
+ flag = 0
+ while True:
+ for i in range(2, prime_number):
+ if prime_number % i == 0:
+ flag = 1
+ break
+ else:
+ pass
+ if flag == 0:
+ yield prime_number
+ else:
+ flag = 0
+ prime_number += 1
+
+
+def alphabet(code='', letters=''):
+ def alphabet_help(letters):
+ for i in range(0, len(letters)):
+ yield letters[i]
+
+ while True:
+ if letters != '':
+ return alphabet_help(letters)
+ if code == 'bg':
+ return alphabet_help("абвгдежзийклмнопрстуфхцчшщъьюя")
+ elif code == 'lat':
+ return alphabet_help("abcdefghijklmnopqrstuvwxyz")
+
+
+def intertwined_sequences(iter_object):
+ work_list = []
+ fibon = fibonacci()
+ prime = primes()
+ for i in range(0, len(iter_object)):
+ if iter_object.__getitem__(i)['sequence'] == 'fibonacci':
+ for j in range(0, iter_object.__getitem__(i)['length']):
+ work_list.append(next(fibon))
+ elif iter_object.__getitem__(i)['sequence'] == 'primes':
+ for j in range(0, iter_object.__getitem__(i)['length']):
+ work_list.append(next(prime))
+ elif iter_object.__getitem__(i)['sequence'] == 'alphabet':
+ if 'letters' in iter_object.__getitem__(i):
+ alpha = alphabet(letters=iter_object.__getitem__(i)['letters'])
+ for j in range(0, iter_object.__getitem__(i)['length']):
+ work_list.append(next(alpha))
+ else:
+ alpha_second = alphabet(code=iter_object.__getitem__(i)['code'])
+ for j in range(0, iter_object.__getitem__(i)['length']):
+ work_list.append(next(alpha_second))
+ return work_list # nai-groznoto parche kod , koeto sym pisal ...no raboti