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

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

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

Резултати

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

Код

def fibonacci():
before = 0
after = 1
while True:
yield after
before = after + before
yield before
after = after + before
def is_prime(num):
for iter in range(2, num):
if num % iter == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(letters=None, **kwargs):
if hasattr(letters, '__iter__'):
for index in range(0, len(letters)):
yield letters[index]
elif kwargs:
if kwargs['code'] == 'lat':
for index in range(ord('a'), ord('z')+1):
yield chr(index)
if kwargs['code'] == 'bg':
for index in range(ord('а'), ord('я')+1):
if chr(index) == 'ы' or chr(index) == 'э':
continue
else:
yield chr(index)
def intertwined_sequences(sequences):
res = []
for item in sequences:
if 'code' not in item:
new_gen = eval('{}()'.format(item['sequence']))
else:
code = "code='" + item['code'] + "'"
new_gen = eval('{}({})'.format(item['sequence'], code))
for i in range(0, item['length']):
res.extend([next(new_gen)])
return res

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

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

----------------------------------------------------------------------
Ran 14 tests in 13.603s

FAILED (errors=6)

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

Светлин обнови решението на 28.03.2015 19:06 (преди над 9 години)

+def fibonacci():
+ before = 0
+ after = 1
+ while True:
+ yield after
+ before = after + before
+ yield before
+ after = after + before
+
+
+def is_prime(n):
+ for iter in range(2, n):
+ if n % iter == 0:
+ return False
+ return True
+
+
+def primes():
+ number = 2
+ while True:
+ if is_prime(number):
+ yield number
+ number += 1
+
+
+def alphabet(letters=None, **kwargs):
+ if hasattr(letters, '__iter__'):
+ for index in range(0, len(letters)):
+ yield letters[index]
+
+ elif kwargs:
+ if kwargs['code'] == 'lat':
+ for i in range(ord('a'), ord('z')+1):
+ yield chr(i)
+ if kwargs['code'] == 'bg':
+ for i in range(ord('а'), ord('я')+1):
+ yield chr(i)

Светлин обнови решението на 28.03.2015 19:07 (преди над 9 години)

def fibonacci():
before = 0
after = 1
while True:
yield after
before = after + before
yield before
after = after + before
-def is_prime(n):
- for iter in range(2, n):
- if n % iter == 0:
+def is_prime(num):
+ for iter in range(2, num):
+ if num % iter == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(letters=None, **kwargs):
if hasattr(letters, '__iter__'):
for index in range(0, len(letters)):
yield letters[index]
elif kwargs:
if kwargs['code'] == 'lat':
- for i in range(ord('a'), ord('z')+1):
- yield chr(i)
+ for index in range(ord('a'), ord('z')+1):
+ yield chr(index)
if kwargs['code'] == 'bg':
- for i in range(ord('а'), ord('я')+1):
- yield chr(i)
+ for index in range(ord('а'), ord('я')+1):
+ yield chr(index)
  • 'ы' и 'э' са букви от кирилицата, но не и от българската азбука.
  • викането на len върху произволни итерувми обекти не е добра идея, тъй като може и да няма начин да се разбере колко са дълги.

Светлин обнови решението на 03.04.2015 03:31 (преди над 9 години)

def fibonacci():
before = 0
after = 1
while True:
yield after
before = after + before
yield before
after = after + before
def is_prime(num):
for iter in range(2, num):
if num % iter == 0:
return False
return True
def primes():
number = 2
while True:
if is_prime(number):
yield number
number += 1
def alphabet(letters=None, **kwargs):
if hasattr(letters, '__iter__'):
for index in range(0, len(letters)):
yield letters[index]
elif kwargs:
if kwargs['code'] == 'lat':
for index in range(ord('a'), ord('z')+1):
yield chr(index)
if kwargs['code'] == 'bg':
for index in range(ord('а'), ord('я')+1):
- yield chr(index)
+ if chr(index) == 'ы' or chr(index) == 'э':
+ continue
+ else:
+ yield chr(index)
+
+
+def intertwined_sequences(sequences):
+ res = []
+ for item in sequences:
+ if 'code' not in item:
+ new_gen = eval('{}()'.format(item['sequence']))
+ else:
+ code = "code='" + item['code'] + "'"
+ new_gen = eval('{}({})'.format(item['sequence'], code))
+ for i in range(0, item['length']):
+ res.extend([next(new_gen)])
+ return res