Решение на Генератори и итератори от Мария Димитрова

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

Към профила на Мария Димитрова

Резултати

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

Код

import math
def fibonacci():
value_one , value_two = 1, 1
while True:
yield value_one
value_one, value_two = value_two, value_one + value_two
def primes():
counter = 2
while True:
isPrime = True
for x in range(2, int(math.sqrt(counter) + 1)):
if counter % x == 0:
isPrime = False
break
if isPrime:
yield counter
counter += 1
def alphabet(code = "", letters = ""):
if letters is not "":
i = 0
one = 1
while one == 1:
yield letters[i]
i = i + 1
else:
if code == 'lat':
for a in range(97, 122):
yield chr(a)
a = a + 1
if code == 'bg':
for b in range(1072, 1103):
yield chr(b)
b = b + 1
def intertwined_sequences(*args):
result = []
fibonacci_numbers = fibonacci()
prime = primes()
bulgarian_cyrillic = alphabet(code='bg')
latin = alphabet(code='lat')
for parameters in args:
for parameter in parameters:
if parameter.get('sequence') == 'fibonacci':
for i in range(0,parameter.get('length')):
result.append(next(fibonacci_numbers))
if parameter.get('sequence') == 'primes':
for i in range(0,parameter.get('length')):
result.append(next(prime))
if parameter.get('sequence') == 'alphabet':
if parameter.get('code') == 'bg':
for i in range(0,parameter.get('length')):
result.append(next(bulgarian_cyrillic))
elif parameter.get('code') == 'lat':
for i in range(0,parameter.get('length')):
result.append(next(latin))
if parameter.get('letters'):
for i in range(0,parameter.get('length')):
result.append(parameter.get('letters')[i])
return result

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

EE.E..EE..EE..
======================================================================
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_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_lat_alphabet (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_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 14.195s

FAILED (errors=7)

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

Мария обнови решението на 03.04.2015 15:02 (преди над 9 години)

+import math
+
+def fibonacci():
+ value_one , value_two = 1, 1
+ while True:
+ yield value_one
+ value_one, value_two = value_two, value_one + value_two
+
+
+
+def primes():
+ counter = 2
+ while True:
+ isPrime = True
+ for x in range(2, int(math.sqrt(counter) + 1)):
+ if counter % x == 0:
+ isPrime = False
+ break
+ if isPrime:
+ yield counter
+ counter += 1
+
+
+
+def alphabet(code = "", letters = ""):
+ if letters is not "":
+ i = 0
+ one = 1
+ while one == 1:
+ yield letters[i]
+ i = i + 1
+ else:
+ if code == 'lat':
+ for a in range(97, 122):
+ yield chr(a)
+ a = a + 1
+ if code == 'bg':
+ for b in range(1072, 1103):
+ yield chr(b)
+ b = b + 1
+
+
+
+def intertwined_sequences(*args):
+ result = []
+ fibonacci_numbers = fibonacci()
+ prime = primes()
+ bulgarian_cyrillic = alphabet(code='bg')
+ latin = alphabet(code='lat')
+
+ for parameters in args:
+ for parameter in parameters:
+ if parameter.get('sequence') == 'fibonacci':
+ for i in range(0,parameter.get('length')):
+ result.append(next(fibonacci_numbers))
+ if parameter.get('sequence') == 'primes':
+ for i in range(0,parameter.get('length')):
+ result.append(next(prime))
+ if parameter.get('sequence') == 'alphabet':
+ if parameter.get('code') == 'bg':
+ for i in range(0,parameter.get('length')):
+ result.append(next(bulgarian_cyrillic))
+ elif parameter.get('code') == 'lat':
+ for i in range(0,parameter.get('length')):
+ result.append(next(latin))
+ if parameter.get('letters'):
+ for i in range(0,parameter.get('length')):
+ result.append(parameter.get('letters')[i])
+ return result