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

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

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

Резултати

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

Код

import itertools
def fibonacci():
a, b = 1, 1
while True:
yield a
a, b = b, a + b
def primes():
return (n for n in itertools.count(2)
if all(n % div for div in range(2, n // 2 + 1)))
BG_ALPHABET = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
"п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"]
def alphabet(**kwargs):
if 'code' in kwargs.keys() and 'letters' not in kwargs.keys():
if kwargs['code'] is 'lat':
return (chr(letter) for letter in range(97, 123))
elif kwargs['code'] is 'bg':
return (letter for letter in BG_ALPHABET)
elif 'letters' in kwargs.keys():
return (letter for letter in kwargs['letters'])
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 intertwined_sequences(arg, **kwargs):
functions = {
'fibonacci': fibonacci, 'primes': primes, 'alphabet': alphabet}
for kwarg_value in kwargs.values():
for k, v in kwarg_value.items():
functions[k] = v
generators = {}
for element in arg:
if element['sequence'] not in generators.keys():
if element['sequence'] == 'alphabet' and 'code' in element.keys():
generators['alphabet'] = functions[
'alphabet'](code=element['code'])
elif element['sequence'] == 'alphabet' and 'letters' in element.keys():
generators['alphabet'] = functions[
'alphabet'](letters=element['letters'])
else:
to_be_passed = []
for key, value in element.items():
if key != 'sequence' and key != 'length':
to_be_passed.append(value)
generators[element['sequence']] = functions[
element['sequence']](*tuple(to_be_passed))
for element in arg:
for i, j in enumerate(generators[element['sequence']]):
yield j
if i >= element['length'] - 1:
break

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

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

----------------------------------------------------------------------
Ran 14 tests in 6.972s

FAILED (errors=3)

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

Теодора обнови решението на 01.04.2015 22:55 (преди над 9 години)

+import itertools
+
+
+def fibonacci():
+ a, b = 1, 1
+ while True:
+ yield a
+ a, b = b, a + b
+
+
+def primes():
+ return (n for n in itertools.count(2)
+ if all(n % div for div in range(2, n // 2 + 1)))
+
+
+BG_ALPHABET = ["а", "б", "в", "г", "д", "е", "ж", "з", "и", "й", "к", "л", "м", "н", "о",
+ "п", "р", "с", "т", "у", "ф", "х", "ц", "ч", "ш", "щ", "ъ", "ь", "ю", "я"]
+
+
+def alphabet(**kwargs):
+ if 'code' in kwargs.keys() and 'letters' not in kwargs.keys():
+ if kwargs['code'] is 'lat':
+ return (chr(letter) for letter in range(97, 123))
+ elif kwargs['code'] is 'bg':
+ return (letter for letter in BG_ALPHABET)
+ elif 'letters' in kwargs.keys():
+ return (letter for letter in kwargs['letters'])
+
+
+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 intertwined_sequences(arg, **kwargs):
+ functions = {
+ 'fibonacci': fibonacci, 'primes': primes, 'alphabet': alphabet}
+ for kwarg_value in kwargs.values():
+ for k, v in kwarg_value.items():
+ functions[k] = v
+ generators = {}
+ for element in arg:
+ if element['sequence'] not in generators.keys():
+ if element['sequence'] == 'alphabet' and 'code' in element.keys():
+ generators['alphabet'] = functions[
+ 'alphabet'](code=element['code'])
+ elif element['sequence'] == 'alphabet' and 'letters' in element.keys():
+ generators['alphabet'] = functions[
+ 'alphabet'](letters=element['letters'])
+ else:
+ to_be_passed = []
+ for key, value in element.items():
+ if key != 'sequence' and key != 'length':
+ to_be_passed.append(value)
+ generators[element['sequence']] = functions[
+ element['sequence']](*tuple(to_be_passed))
+
+ for element in arg:
+ for i, j in enumerate(generators[element['sequence']]):
+ yield j
+ if i >= element['length'] - 1:
+ break