Решение на Генератори и итератори от Михаил Здравков

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

Към профила на Михаил Здравков

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 12 успешни тест(а)
  • 2 неуспешни тест(а)

Код

def fibonacci():
yield(1)
last, num = 1, 1
while True:
yield(num)
last, num = num, last + num
def primes():
yield(2)
num = 2
while True:
num += 1
if len([x for x in range(2, num) if num % x == 0]) == 0:
yield(num)
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
if letter.lower() == letter:
yield(letter)
elif 'code' in kwargs:
letter, end = None, None
if kwargs['code'] == 'lat':
letter, end = 'a', 'z'
elif kwargs['code'] == 'bg':
letter, end = 'а', 'я'
else:
return
while letter != chr(ord(end) + 1):
if letter == 'э' or letter == 'ы':
letter = chr(ord(letter) + 1)
yield(letter)
letter = chr(ord(letter) + 1)
def intertwined_sequences(format, **kwargs):
generators = kwargs.get('generator_definitions', {})
excluded = ['sequence', 'length']
for name, func in generators.items():
for x in format:
if x['sequence'] == name:
args = {k: v for k, v in x.items() if k not in excluded}
generators[name] = iter(func(**args))
break
for elem in format:
seq = elem['sequence']
if seq not in generators:
if seq == 'alphabet':
generators[seq] = alphabet(**elem)
else:
generators[seq] = globals()[seq]()
for _ in range(elem['length']):
yield(next(generators[seq]))

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

.......E....E.
======================================================================
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_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 4.285s

FAILED (errors=2)

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

Михаил обнови решението на 02.04.2015 03:05 (преди над 9 години)

+def fibonacci():
+ yield(1)
+ last, num = 1, 1
+ while True:
+ yield(num)
+ tmp = num
+ num += last
+ last = tmp
+
+
+def primes():
+ yield(2)
+ num = 2
+ while True:
+ num += 1
+ if len([x for x in range(2, num) if num % x == 0]) == 0:
+ yield(num)
+
+
+def alphabet(**kwargs):
+ if 'letters' in kwargs:
+ for letter in kwargs['letters']:
+ if letter.lower() == letter:
+ yield(letter)
+ elif 'code' in kwargs:
+ letter, end = None, None
+ if kwargs['code'] == 'lat':
+ letter, end = 'a', 'z'
+ elif kwargs['code'] == 'bg':
+ letter, end = 'а', 'я'
+ else:
+ return
+ while letter != chr(ord(end) + 1):
+ if letter == 'э' or letter == 'ы':
+ letter = chr(ord(letter) + 1)
+ yield(letter)
+ letter = chr(ord(letter) + 1)
+
+
+def intertwined_sequences(format, **kwargs):
+ defs = kwargs.get('generator_definitions', {})
+ excluded = ['sequence', 'length']
+ for name, func in defs.items():
+ for x in format:
+ if x['sequence'] == name:
+ args = {k: v for k, v in x.items() if k not in excluded}
+ defs[name] = func, args
+ break
+ generators = {k: v[0](**v[1]) for k, v in defs.items()}
+ for elem in format:
+ seq = elem['sequence']
+ if seq not in generators:
+ if seq == 'alphabet':
+ generators[seq] = alphabet(**elem)
+ else:
+ generators[seq] = globals()[seq]()
+ for _ in range(elem['length']):
+ yield(next(generators[seq]))

Михаил обнови решението на 02.04.2015 03:10 (преди над 9 години)

def fibonacci():
yield(1)
last, num = 1, 1
while True:
yield(num)
- tmp = num
- num += last
- last = tmp
+ last, num = num, last + num
def primes():
yield(2)
num = 2
while True:
num += 1
if len([x for x in range(2, num) if num % x == 0]) == 0:
yield(num)
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
if letter.lower() == letter:
yield(letter)
elif 'code' in kwargs:
letter, end = None, None
if kwargs['code'] == 'lat':
letter, end = 'a', 'z'
elif kwargs['code'] == 'bg':
letter, end = 'а', 'я'
else:
return
while letter != chr(ord(end) + 1):
if letter == 'э' or letter == 'ы':
letter = chr(ord(letter) + 1)
yield(letter)
letter = chr(ord(letter) + 1)
def intertwined_sequences(format, **kwargs):
defs = kwargs.get('generator_definitions', {})
excluded = ['sequence', 'length']
for name, func in defs.items():
for x in format:
if x['sequence'] == name:
args = {k: v for k, v in x.items() if k not in excluded}
defs[name] = func, args
break
generators = {k: v[0](**v[1]) for k, v in defs.items()}
for elem in format:
seq = elem['sequence']
if seq not in generators:
if seq == 'alphabet':
generators[seq] = alphabet(**elem)
else:
generators[seq] = globals()[seq]()
for _ in range(elem['length']):
yield(next(generators[seq]))

Михаил обнови решението на 02.04.2015 22:59 (преди над 9 години)

def fibonacci():
yield(1)
last, num = 1, 1
while True:
yield(num)
last, num = num, last + num
def primes():
yield(2)
num = 2
while True:
num += 1
if len([x for x in range(2, num) if num % x == 0]) == 0:
yield(num)
def alphabet(**kwargs):
if 'letters' in kwargs:
for letter in kwargs['letters']:
if letter.lower() == letter:
yield(letter)
elif 'code' in kwargs:
letter, end = None, None
if kwargs['code'] == 'lat':
letter, end = 'a', 'z'
elif kwargs['code'] == 'bg':
letter, end = 'а', 'я'
else:
return
while letter != chr(ord(end) + 1):
if letter == 'э' or letter == 'ы':
letter = chr(ord(letter) + 1)
yield(letter)
letter = chr(ord(letter) + 1)
def intertwined_sequences(format, **kwargs):
- defs = kwargs.get('generator_definitions', {})
+ generators = kwargs.get('generator_definitions', {})
excluded = ['sequence', 'length']
- for name, func in defs.items():
+ for name, func in generators.items():
for x in format:
if x['sequence'] == name:
args = {k: v for k, v in x.items() if k not in excluded}
- defs[name] = func, args
+ generators[name] = iter(func(**args))
break
- generators = {k: v[0](**v[1]) for k, v in defs.items()}
for elem in format:
seq = elem['sequence']
if seq not in generators:
if seq == 'alphabet':
generators[seq] = alphabet(**elem)
else:
generators[seq] = globals()[seq]()
for _ in range(elem['length']):
yield(next(generators[seq]))