Решение на Генератори и итератори от Йордан Пулов

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

Към профила на Йордан Пулов

Резултати

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

Код

def fibonacci():
current = 1
previous = 0
next_fib = 1
yield next_fib
while True:
next_fib = current + previous
yield next_fib
previous = current
current = next_fib
def primes():
# helper func to check prime number
def is_prime(n):
for num in range(2, n):
if n % num == 0:
return False
return True
number_to_check = 2
while True:
if is_prime(number_to_check):
yield number_to_check
number_to_check += 1
def alphabet(code=None, letters=""):
APLTHABET = {
"lat": "abcdefghijklmnopqrstuvwxyz",
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя"
}
if letters == "" and code is not None:
letters = APLTHABET[code]
for letter in letters:
yield letter
def intertwined_sequences(iter_object):
sequences = []
cache_funcs = {}
for kwargs in iter_object:
cache_key = str(kwargs)
sequence = kwargs.pop("sequence", None)
length = kwargs.pop("length", None)
cached_func = cache_funcs.get(cache_key, None)
if cached_func is not None:
func = cached_func
elif len(kwargs) == 0:
func = eval(sequence)()
else:
func_str = "{}(".format(sequence)
for key in kwargs:
func_str = "{}{}='{}', ".format(func_str, key, kwargs[key])
func_str = "{})".format(func_str)
func = eval(func_str)
for _ in range(length):
sequences.append(next(func))
cache_funcs[cache_key] = func
return sequences

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

......EE..EE..
======================================================================
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 9.779s

FAILED (errors=4)

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

Йордан обнови решението на 01.04.2015 23:32 (преди над 9 години)

+
+def fibonacci():
+ current = 1
+ previous = 0
+ next_fib = 1
+ yield next_fib
+ while True:
+ next_fib = current + previous
+ yield next_fib
+ previous = current
+ current = next_fib
+
+
+def primes():
+ # helper func to check prime number
+ def is_prime(n):
+ for num in range(2, n):
+ if n % num == 0:
+ return False
+ return True
+
+ number_to_check = 2
+
+ while True:
+ if is_prime(number_to_check):
+ yield number_to_check
+ number_to_check += 1
+
+
+def alphabet(code=None, letters=""):
+ APLTHABET = {
+ "lat": "abcdefghijklmnopqrstuvwxyz",
+ "bg": "абвгдежзийклмнопрстуфхцчшщъьюя"
+ }
+ if letters == "" and code != "":
+ letters = APLTHABET[code]
+
+ for letter in letters:
+ yield letter
+

Йордан обнови решението на 02.04.2015 23:12 (преди над 9 години)

def fibonacci():
current = 1
previous = 0
next_fib = 1
yield next_fib
while True:
next_fib = current + previous
yield next_fib
previous = current
current = next_fib
def primes():
# helper func to check prime number
def is_prime(n):
for num in range(2, n):
if n % num == 0:
return False
return True
number_to_check = 2
while True:
if is_prime(number_to_check):
yield number_to_check
number_to_check += 1
def alphabet(code=None, letters=""):
APLTHABET = {
"lat": "abcdefghijklmnopqrstuvwxyz",
"bg": "абвгдежзийклмнопрстуфхцчшщъьюя"
}
- if letters == "" and code != "":
+ if letters == "" and code is not None:
letters = APLTHABET[code]
for letter in letters:
yield letter
+
+def intertwined_sequences(iter_object):
+ sequences = []
+ cache_funcs = {}
+ for kwargs in iter_object:
+
+ cache_key = str(kwargs)
+ sequence = kwargs.pop("sequence", None)
+ length = kwargs.pop("length", None)
+ cached_func = cache_funcs.get(cache_key, None)
+
+ if cached_func is not None:
+ func = cached_func
+ elif len(kwargs) == 0:
+ func = eval(sequence)()
+ else:
+ func_str = "{}(".format(sequence)
+ for key in kwargs:
+ func_str = "{}{}='{}', ".format(func_str, key, kwargs[key])
+ func_str = "{})".format(func_str)
+ func = eval(func_str)
+
+ for _ in range(length):
+ sequences.append(next(func))
+
+ cache_funcs[cache_key] = func
+
+ return sequences