Решение на Генератори и итератори от Васил Пачеджиев

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

Към профила на Васил Пачеджиев

Резултати

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

Код

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
import math
def primes():
number = 2
while True:
prime = True
for r in range(2, int(math.sqrt(number) + 1)):
if number % r == 0:
prime = False
break
if prime:
yield number
number += 1
def alphabet(*, code='', letters=''):
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for l in letters:
yield l
def intertwined_sequences(iterative, *, generator_definitions={}):
fib = fibonacci()
pri = primes()
alp_lat = alphabet(code='lat')
alp_bg = alphabet(code='bg')
iters = {}
for key, value in generator_definitions.items():
iters[key] = value()
for func in iterative:
if func['sequence'] == 'fibonacci':
for i in range(func['length']):
yield next(fib)
elif func['sequence'] == 'primes':
for j in range(func['length']):
yield next(pri)
elif func['sequence'] == 'alphabet':
if func['code'] == 'lat':
for l in range(func['length']):
yield next(alp_lat)
elif func['code'] == 'bg':
for l in range(func['length']):
yield next(alp_bg)
else:
for l in range(func['length']):
yield next(iters[func['sequence']])

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

......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 8.202s

FAILED (errors=4)

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

Васил обнови решението на 31.03.2015 23:04 (преди над 9 години)

+def fibonacci():
+
+ first, second = 1, 1
+ while True:
+ yield first
+ first, second = second, first + second
+
+
+import math
+
+
+def primes():
+
+ number = 2
+ while True:
+ prime = True
+ for r in range(2, int(math.sqrt(number) + 1)):
+ if number % r == 0:
+ prime = False
+ break
+ if prime:
+ yield number
+ number += 1
+
+
+def alphabet(code='', letters=''):
+
+ if code == 'lat':
+ letters = 'abcdefghijklmnopqrstuvwxyz'
+ elif code == 'bg':
+ letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+
+ for l in letters:
+ yield l
+
+
+def intertwined_sequences(iterative):
+
+ fib = fibonacci()
+ pri = primes()
+ alp_lat = alphabet(code='lat')
+ alp_bg = alphabet(code='bg')
+
+ for func in iterative:
+ if func['sequence'] == 'fibonacci':
+ for i in range(func['length']):
+ yield next(fib)
+ elif func['sequence'] == 'primes':
+ for j in range(func['length']):
+ yield next(pri)
+ elif func['sequence'] == 'alphabet':
+ if func['code'] == 'lat':
+ for l in range(func['length']):
+ yield next(alp_lat)
+ elif func['code'] == 'bg':
+ for l in range(func['length']):
+ yield next(alp_bg)

Васил обнови решението на 02.04.2015 16:00 (преди над 9 години)

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
import math
def primes():
number = 2
while True:
prime = True
for r in range(2, int(math.sqrt(number) + 1)):
if number % r == 0:
prime = False
break
if prime:
yield number
number += 1
-def alphabet(code='', letters=''):
+def alphabet(*, code='', letters=''):
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
for l in letters:
yield l
def intertwined_sequences(iterative):
fib = fibonacci()
pri = primes()
alp_lat = alphabet(code='lat')
alp_bg = alphabet(code='bg')
for func in iterative:
if func['sequence'] == 'fibonacci':
for i in range(func['length']):
yield next(fib)
elif func['sequence'] == 'primes':
for j in range(func['length']):
yield next(pri)
elif func['sequence'] == 'alphabet':
if func['code'] == 'lat':
for l in range(func['length']):
yield next(alp_lat)
elif func['code'] == 'bg':
for l in range(func['length']):
- yield next(alp_bg)
+ yield next(alp_bg)

Васил обнови решението на 02.04.2015 23:35 (преди над 9 години)

def fibonacci():
first, second = 1, 1
while True:
yield first
first, second = second, first + second
import math
def primes():
number = 2
while True:
prime = True
for r in range(2, int(math.sqrt(number) + 1)):
if number % r == 0:
prime = False
break
if prime:
yield number
number += 1
def alphabet(*, code='', letters=''):
if code == 'lat':
letters = 'abcdefghijklmnopqrstuvwxyz'
elif code == 'bg':
letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
-
for l in letters:
yield l
-def intertwined_sequences(iterative):
+def intertwined_sequences(iterative, *, generator_definitions={}):
fib = fibonacci()
pri = primes()
alp_lat = alphabet(code='lat')
alp_bg = alphabet(code='bg')
+ iters = {}
+ for key, value in generator_definitions.items():
+ iters[key] = value()
for func in iterative:
if func['sequence'] == 'fibonacci':
for i in range(func['length']):
yield next(fib)
elif func['sequence'] == 'primes':
for j in range(func['length']):
yield next(pri)
elif func['sequence'] == 'alphabet':
if func['code'] == 'lat':
for l in range(func['length']):
yield next(alp_lat)
elif func['code'] == 'bg':
for l in range(func['length']):
yield next(alp_bg)
+ else:
+ for l in range(func['length']):
+ yield next(iters[func['sequence']])