Решение на Генератори и итератори от Константин Димитров

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

Към профила на Константин Димитров

Резултати

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

Код

# -*- coding: utf-8 -*-
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
n = 2
primes = []
while True:
is_prime = True
for p in primes:
if n % p == 0:
is_prime = False
break
if is_prime:
primes.append(n)
yield n
n += 1
DEFAULT_ALPHABETS = {
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
'lat': 'abcdefghijklmnopqrstuvwxyz',
}
def alphabet(code='', letters=''):
used_alphabet = letters if letters else DEFAULT_ALPHABETS.get(code)
current_index = 0
while True:
yield used_alphabet[current_index]
current_index += 1
def intertwined_sequences(func_calls):
result = []
called_functions = {}
for call in func_calls:
if 'sequence' not in called_functions:
sequence = globals()[call['sequence']]
if call['sequence'] is 'alphabet':
kwargs = {
'code': call.get('code', ''),
'letters': call.get('letters', ''),
}
method = sequence(**kwargs)
else:
method = sequence()
called_functions[call['sequence']] = method
for call in func_calls:
for i in range(call['length']):
result.append(next(called_functions[call['sequence']]))
return result

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

.E....EE..EE..
======================================================================
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_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 10.731s

FAILED (errors=5)

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

Константин обнови решението на 03.04.2015 09:23 (преди над 9 години)

+# -*- coding: utf-8 -*-
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ n = 2
+ primes = []
+ while True:
+ is_prime = True
+ for p in primes:
+ if n % p == 0:
+ is_prime = False
+ break
+
+ if is_prime:
+ primes.append(n)
+ yield n
+
+ n += 1
+
+
+DEFAULT_ALPHABETS = {
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя',
+ 'lat': 'abcdefghijklmnopqrstuvwxyz',
+}
+
+
+def alphabet(code='', letters=''):
+ used_alphabet = letters if letters else DEFAULT_ALPHABETS.get(code)
+ current_index = 0
+ while True:
+ yield used_alphabet[current_index]
+ current_index += 1
+
+
+def intertwined_sequences(func_calls):
+ result = []
+ called_functions = {}
+ for call in func_calls:
+ if 'sequence' not in called_functions:
+ sequence = globals()[call['sequence']]
+
+ if call['sequence'] is 'alphabet':
+ kwargs = {
+ 'code': call.get('code', ''),
+ 'letters': call.get('letters', ''),
+ }
+
+ method = sequence(**kwargs)
+ else:
+ method = sequence()
+
+ called_functions[call['sequence']] = method
+
+ for call in func_calls:
+ for i in range(call['length']):
+ result.append(next(called_functions[call['sequence']]))
+
+ return result