Решение на Генератори и итератори от Aнтония Чекръкчиева

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

Към профила на Aнтония Чекръкчиева

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 10 успешни тест(а)
  • 4 неуспешни тест(а)

Код

import itertools
import inspect
ALPHABETS = {'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
def fibonacci():
first, second = 1, 1
yield first
yield second
while True:
first, second = second, first + second
yield second
def primes():
number = 2
while True:
if all([number % divisor for divisor in range(2, number)]):
yield number
number += 1
def alphabet(**kwargs):
if kwargs.get('letters'):
return iter(kwargs.get('letters'))
if kwargs.get('code'):
return iter(ALPHABETS[kwargs.get('code')])
def intertwined_sequences(sequence_objects, generator_definitions=None):
functions = {'fibonacci': fibonacci(),
'primes': primes(),
'alphabet': alphabet(code='bg'),
'alphabetLat': alphabet(code='lat')}
if generator_definitions:
for key, values in generator_definitions.items():
func_args = find_function_argument(sequence_objects, values)
if func_args:
functions[key] = iter(values(func_args))
else:
functions[key] = iter(values())
for objects in sequence_objects:
for i in range(objects.get('length')):
if objects['sequence'] == 'alphabet' and objects['code'] == 'lat':
yield next(functions['alphabetLat'])
else:
yield next(functions[objects['sequence']])
def find_function_argument(sequence_objects, function):
function_argument = inspect.getargspec(function)[0]
if function_argument:
for seq in sequence_objects:
if function_argument[0] in seq:
return seq[function_argument[0]]

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

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

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

----------------------------------------------------------------------
Ran 14 tests in 8.163s

FAILED (errors=4)

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

Aнтония обнови решението на 03.04.2015 00:08 (преди над 9 години)

+import itertools
+import inspect
+
+ALPHABETS = {'lat': 'abcdefghijklmnopqrstuvwxyz',
+ 'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
+
+
+def fibonacci():
+ first, second = 1, 1
+ yield first
+ yield second
+ while True:
+ first, second = second, first + second
+ yield second
+
+
+def primes():
+ number = 2
+ while True:
+ if all([number % divisor for divisor in range(2, number)]):
+ yield number
+ number += 1
+
+
+def alphabet(**kwargs):
+ if kwargs.get('letters'):
+ return iter(kwargs.get('letters'))
+ if kwargs.get('code'):
+ return iter(ALPHABETS[kwargs.get('code')])
+
+
+def intertwined_sequences(sequence_objects, generator_definitions=None):
+ functions = {'fibonacci': fibonacci(),
+ 'primes': primes(),
+ 'alphabet': alphabet(code='bg')}
+
+ if generator_definitions:
+ for key, values in generator_definitions.items():
+ func_args = find_function_argument(sequence_objects, values)
+ if func_args:
+ functions[key] = iter(values(func_args))
+ else:
+ functions[key] = iter(values())
+
+ for objects in sequence_objects:
+ for i in range(objects.get('length')):
+ yield next(functions[objects['sequence']])
+
+
+def find_function_argument(sequence_objects, function):
+ function_argument = inspect.getargspec(function)[0]
+ if function_argument:
+ for seq in sequence_objects:
+ if function_argument[0] in seq:
+ return seq[function_argument[0]]

Aнтония обнови решението на 03.04.2015 01:22 (преди над 9 години)

import itertools
import inspect
ALPHABETS = {'lat': 'abcdefghijklmnopqrstuvwxyz',
'bg': 'абвгдежзийклмнопрстуфхцчшщъьюя'}
def fibonacci():
first, second = 1, 1
yield first
yield second
while True:
first, second = second, first + second
yield second
def primes():
number = 2
while True:
if all([number % divisor for divisor in range(2, number)]):
yield number
number += 1
def alphabet(**kwargs):
if kwargs.get('letters'):
return iter(kwargs.get('letters'))
if kwargs.get('code'):
return iter(ALPHABETS[kwargs.get('code')])
def intertwined_sequences(sequence_objects, generator_definitions=None):
functions = {'fibonacci': fibonacci(),
'primes': primes(),
- 'alphabet': alphabet(code='bg')}
+ 'alphabet': alphabet(code='bg'),
+ 'alphabetLat': alphabet(code='lat')}
if generator_definitions:
for key, values in generator_definitions.items():
func_args = find_function_argument(sequence_objects, values)
if func_args:
functions[key] = iter(values(func_args))
else:
functions[key] = iter(values())
for objects in sequence_objects:
for i in range(objects.get('length')):
- yield next(functions[objects['sequence']])
+ if objects['sequence'] == 'alphabet' and objects['code'] == 'lat':
+ yield next(functions['alphabetLat'])
+ else:
+ yield next(functions[objects['sequence']])
def find_function_argument(sequence_objects, function):
function_argument = inspect.getargspec(function)[0]
if function_argument:
for seq in sequence_objects:
if function_argument[0] in seq:
return seq[function_argument[0]]