Решение на Генератори и итератори от Мартин Стоев

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

Към профила на Мартин Стоев

Резултати

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

Код

def fibonacci():
first = 0
second = 1
while True:
yield second
second = first + second
first = second - first
def is_Prime(number):
interval = range(2, number // 2 + 1)
result = [count for count in interval if not number % count]
return len(result) == 0
def primes():
prime = 2
while True:
if is_Prime(prime):
yield prime
prime += 1
def alphabet(*, code='lat', letters=''):
done_letters = False
for letter in letters:
yield letter
done_letters = True
if done_letters:
return
if code == 'bg':
for letter in range(1072, 1099):
yield chr(letter)
yield 'ь'
yield 'ю'
yield 'я'
for letter in range(97, 123):
yield chr(letter)
def intertwined_sequences(data, generator_definitions=dict()):
elements = iter(data)
generator_definitions['fibonacci'] = fibonacci
generator_definitions['primes'] = primes
generator_definitions['alphabet'] = alphabet
functions = dict()
while True:
current = next(elements)
sequence = current['sequence']
if sequence not in functions:
key = ''
for argument in current:
if argument != 'sequence' and argument != 'length':
key = argument
if key != '':
if sequence == 'alphabet':
if key == 'code':
value = alphabet(code=current['code'])
else:
value = alphabet(letters=current['letters'])
else:
help_function = generator_definitions[sequence]
value = help_function(current[key])
else:
value = generator_definitions[sequence]()
functions[sequence] = value
for count in range(current['length']):
yield next(functions[current['sequence']])

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

......E...E.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_kwargs_generator (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 6.153s

FAILED (errors=3)

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

Мартин обнови решението на 29.03.2015 16:22 (преди над 9 години)

+def fibonacci():
+ first = 0
+ second = 1
+ while True:
+ yield second
+ second = first + second
+ first = second - first
+
+
+def is_Prime(number):
+ interval = range(2, number // 2 + 1)
+ result = [count for count in interval if not number % count]
+ return len(result) == 0
+
+
+def primes():
+ prime = 2
+ while True:
+ if is_Prime(prime):
+ yield prime
+ prime += 1
+
+
+def alphabet(code='lat', letters=''):
+ if len(letters) != 0:
+ return iter(letters)
+
+ if code == 'bg':
+ return (chr(element) for element in range(1072, 1104))
+
+ return (chr(element) for element in range(97, 123))
+
+
+def intertwined_sequences(data):
+ functions = dict()
+ elements = iter(data)
+
+ while True:
+ current = next(elements)
+ sequence = current['sequence']
+
+ if sequence not in functions:
+ if sequence == 'alphabet':
+ functions['alphabet'] = alphabet(current['code'])
+ elif sequence == 'fibonacci':
+ functions['fibonacci'] = fibonacci()
+ elif sequence == 'primes':
+ functions['primes'] = primes()
+
+ for count in range(current['length']):
+ yield next(functions[current['sequence']])
  • 'ы' и 'э' са букви от кирилицата, но не и от българската азбука.
  • за много итеруеми обекти няма разумен начин да се определи дължината им, така че извикването на len върху тях е безмислено. Помисли си как може да не се интересуваш шот дължината на нещо.

Мартин обнови решението на 31.03.2015 22:05 (преди над 9 години)

def fibonacci():
first = 0
second = 1
while True:
yield second
second = first + second
first = second - first
def is_Prime(number):
interval = range(2, number // 2 + 1)
result = [count for count in interval if not number % count]
return len(result) == 0
def primes():
prime = 2
while True:
if is_Prime(prime):
yield prime
prime += 1
def alphabet(code='lat', letters=''):
- if len(letters) != 0:
- return iter(letters)
+ done_letters = False
+ for letter in letters:
+ yield letter
+ done_letters = True
+ if done_letters:
+ return
+
if code == 'bg':
- return (chr(element) for element in range(1072, 1104))
+ for letter in range(1072, 1099):
+ yield chr(letter)
+ yield 'ь'
+ yield 'ю'
+ yield 'я'
- return (chr(element) for element in range(97, 123))
+ for letter in range(97, 123):
+ yield chr(letter)
-def intertwined_sequences(data):
- functions = dict()
+def intertwined_sequences(data, generator_definitions=dict()):
elements = iter(data)
+ generator_definitions['fibonacci'] = fibonacci
+ generator_definitions['primes'] = primes
+ generator_definitions['alphabet'] = alphabet
+ functions = dict()
while True:
current = next(elements)
sequence = current['sequence']
if sequence not in functions:
- if sequence == 'alphabet':
- functions['alphabet'] = alphabet(current['code'])
- elif sequence == 'fibonacci':
- functions['fibonacci'] = fibonacci()
- elif sequence == 'primes':
- functions['primes'] = primes()
+ arg = ''
+ for key in current:
+ if key != 'sequence' and key != 'length':
+ arg = key
+
+ if arg != '':
+ value = generator_definitions[sequence](current[arg])
+ functions[sequence] = value
+ else:
+ value = generator_definitions[sequence]()
+ functions[sequence] = value
for count in range(current['length']):
yield next(functions[current['sequence']])

Мартин обнови решението на 03.04.2015 11:23 (преди над 9 години)

def fibonacci():
first = 0
second = 1
while True:
yield second
second = first + second
first = second - first
def is_Prime(number):
interval = range(2, number // 2 + 1)
result = [count for count in interval if not number % count]
return len(result) == 0
def primes():
prime = 2
while True:
if is_Prime(prime):
yield prime
prime += 1
-def alphabet(code='lat', letters=''):
+def alphabet(*, code='lat', letters=''):
done_letters = False
for letter in letters:
yield letter
done_letters = True
if done_letters:
return
if code == 'bg':
for letter in range(1072, 1099):
yield chr(letter)
yield 'ь'
yield 'ю'
yield 'я'
for letter in range(97, 123):
yield chr(letter)
def intertwined_sequences(data, generator_definitions=dict()):
elements = iter(data)
generator_definitions['fibonacci'] = fibonacci
generator_definitions['primes'] = primes
generator_definitions['alphabet'] = alphabet
functions = dict()
while True:
current = next(elements)
sequence = current['sequence']
if sequence not in functions:
- arg = ''
- for key in current:
- if key != 'sequence' and key != 'length':
- arg = key
+ key = ''
+ for argument in current:
+ if argument != 'sequence' and argument != 'length':
+ key = argument
- if arg != '':
- value = generator_definitions[sequence](current[arg])
- functions[sequence] = value
+ if key != '':
+ if sequence == 'alphabet':
+ if key == 'code':
+ value = alphabet(code=current['code'])
+ else:
+ value = alphabet(letters=current['letters'])
+ else:
+ help_function = generator_definitions[sequence]
+ value = help_function(current[key])
else:
value = generator_definitions[sequence]()
- functions[sequence] = value
+
+ functions[sequence] = value
for count in range(current['length']):
yield next(functions[current['sequence']])