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

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

Към профила на Христо Тодоров

Резултати

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

Код

from math import *
def fibonacci():
n = 0
while True:
fi = (1 + sqrt(5)) / 2
yield int((pow(fi, n) - pow(-fi, -n)) / sqrt(5))
n += 1
def is_prime(number):
for i in range(2, number):
if number % 2 == 0:
return False
return True
def primes():
n = 2
while True:
if is_prime(n):
yield n
n += 1
def alphabet(code='', letters=''):
if code != '':
if code == 'bg':
lower = [chr(i) for i in range(ord('а'), ord('я') + 1)]
lower.remove('ы')
lower.remove('ь')
lower.remove('э')
return iter(lower)
else:
lower = [chr(i) for i in range(ord('a'), ord('z') + 1)]
lower.remove('j')
lower.remove('u')
lower.remove('w')
return iter(lower)
elif letters != '' and code == '':
return iter(letters)
def intertwined_sequences(iter_obj, generator_definitions=dict()):
mix_iterator = list()
dictionary = dict()
dictionary.update({'fibonacci': fibonacci})
dictionary.update({'primes': primes})
dictionary.update({'alphabet': alphabet})
dictionary.update(generator_definitions)
for item in iter_obj:
if item['sequence'] != 'alphabet':
seq = dictionary[item['sequence']]()
mix_iterator.extend([next(seq) for _ in range(item['length'])])
else:
if 'code' in item:
seq = dictionary['alphabet'](code=item['code'])
mix_iterator.extend([next(seq) for _ in range(item['length'])])
else:
seq = dictionary['alphabet'](letters=item['letters'])
mix_iterator.extend([next(seq) for _ in range(item['length'])])
return iter(mix_iterator)

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

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

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

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

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 24.484s

FAILED (errors=12)

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

Христо обнови решението на 03.04.2015 16:52 (преди над 9 години)

+from math import *
+
+
+def fibonacci():
+ n = 0
+ while True:
+ fi = (1 + sqrt(5)) / 2
+ yield int((pow(fi, n) - pow(-fi, -n)) / sqrt(5))
+ n += 1
+
+
+def is_prime(number):
+ for i in range(2, number):
+ if number % 2 == 0:
+ return False
+ return True
+
+
+def primes():
+ n = 2
+ while True:
+ if is_prime(n):
+ yield n
+ n += 1
+
+
+def alphabet(code='', letters=''):
+ if code != '':
+ if code == 'bg':
+ lower = [chr(i) for i in range(ord('а'), ord('я') + 1)]
+ lower.remove('ы')
+ lower.remove('ь')
+ lower.remove('э')
+ return iter(lower)
+ else:
+ lower = [chr(i) for i in range(ord('a'), ord('z') + 1)]
+ lower.remove('j')
+ lower.remove('u')
+ lower.remove('w')
+ return iter(lower)
+ elif letters != '' and code == '':
+ return iter(letters)
+
+
+def intertwined_sequences(iter_obj, generator_definitions=dict()):
+ mix_iterator = list()
+ dictionary = dict()
+ dictionary.update({'fibonacci': fibonacci})
+ dictionary.update({'primes': primes})
+ dictionary.update({'alphabet': alphabet})
+ dictionary.update(generator_definitions)
+ for item in iter_obj:
+ if item['sequence'] != 'alphabet':
+ seq = dictionary[item['sequence']]()
+ mix_iterator.extend([next(seq) for _ in range(item['length'])])
+ else:
+ if 'code' in item:
+ seq = dictionary['alphabet'](code=item['code'])
+ mix_iterator.extend([next(seq) for _ in range(item['length'])])
+ else:
+ seq = dictionary['alphabet'](letters=item['letters'])
+ mix_iterator.extend([next(seq) for _ in range(item['length'])])
+ return iter(mix_iterator)