Решение на Генератори и итератори от Ивайло Цанков

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

Към профила на Ивайло Цанков

Резултати

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

Код

def fibonacci():
current_number, next_number = 1, 1
while True:
yield current_number
current_number, next_number = next_number, current_number + next_number
def primes():
def is_prime(n):
if n <= 3:
return n >= 2
if n % 2 == 0 or n % 3 == 0:
return False
for i in range(5, int(n ** 0.5) + 1, 6):
if n % i == 0 or n % (i + 2) == 0:
return False
return True
number = 0
while True:
number += 1
if is_prime(number):
yield number
def alphabet(code=None, letters=None):
if letters:
for letter in letters:
yield letter
# if both letters and code are defined this will ignore code
return StopIteration
else:
lower_bound = 0x61 if code == "lat" else 0x430 # else "bg"
upper_bound = 0x7B if code == "lat" else 0x450 # else "bg"
for letter_hex in range(lower_bound, upper_bound):
# 0x44D and 0x44B are from russian alphabet
if letter_hex != 0x44D or letter_hex != 0x44B:
yield chr(letter_hex)
return StopIteration
def intertwined_sequences(configurations, generator_definitions=[]):
def get_generator(name):
return globals()[name]
generators = {}
for config in configurations:
name = config["sequence"]
length = config["length"]
del(config["sequence"])
del(config["length"])
# create generator from definition on the fly
if name in generator_definitions and name not in generators:
if len(config) == 0:
generators[name] = iter(generator_definitions[name]())
else:
generators[name] = iter(
generator_definitions[name](*config.values()))
# create generator if not exists
if name not in generators:
if len(config) == 0:
generators[name] = get_generator(name)()
else:
generators[name] = get_generator(name)(config_params)
generator = generators[name]
for i in range(length):
yield(next(generator))
return StopIteration

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

E......EEEEE..
======================================================================
ERROR: test_bg_alphabet (test.TestAlphabet)
----------------------------------------------------------------------
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 (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 12.166s

FAILED (errors=6)

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

Ивайло обнови решението на 03.04.2015 11:58 (преди над 9 години)

+def fibonacci():
+ current_number, next_number = 1, 1
+
+ while True:
+ yield current_number
+ current_number, next_number = next_number, current_number + next_number
+
+
+def primes():
+ def is_prime(n):
+ if n <= 3:
+ return n >= 2
+ if n % 2 == 0 or n % 3 == 0:
+ return False
+ for i in range(5, int(n ** 0.5) + 1, 6):
+ if n % i == 0 or n % (i + 2) == 0:
+ return False
+ return True
+
+ number = 0
+ while True:
+ number += 1
+ if is_prime(number):
+ yield number
+
+
+def alphabet(code=None, letters=None):
+ if letters:
+ for letter in letters:
+ yield letter
+
+ # if both letters and code are defined this will ignore code
+ return StopIteration
+ else:
+ lower_bound = 0x61 if code == "lat" else 0x430 # else "bg"
+ upper_bound = 0x7B if code == "lat" else 0x450 # else "bg"
+
+ for letter_hex in range(lower_bound, upper_bound):
+ # 0x44D and 0x44B are from russian alphabet
+ if letter_hex != 0x44D or letter_hex != 0x44B:
+ yield chr(letter_hex)
+
+ return StopIteration
+
+
+def intertwined_sequences(configurations, generator_definitions=[]):
+ def get_generator(name):
+ return globals()[name]
+
+ generators = {}
+ for config in configurations:
+ name = config["sequence"]
+ length = config["length"]
+ del(config["sequence"])
+ del(config["length"])
+
+ # create generator from definition on the fly
+ if name in generator_definitions and name not in generators:
+ if len(config) == 0:
+ generators[name] = iter(generator_definitions[name]())
+ else:
+ generators[name] = iter(
+ generator_definitions[name](*config.values()))
+
+ # create generator if not exists
+ if name not in generators:
+ if len(config) == 0:
+ generators[name] = get_generator(name)()
+ else:
+ generators[name] = get_generator(name)(config_params)
+
+ generator = generators[name]
+
+ for i in range(length):
+ yield(next(generator))
+
+ return StopIteration