Решение на Генератори и итератори от Вера Бойчева

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

Към профила на Вера Бойчева

Резултати

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

Код

def fibonacci():
counter = 0
while True:
if counter == 0 or counter == 1:
yield 1
counter += 1
else:
number1 = 1
number2 = 1
while True:
yield number1 + number2
temp = number2
number2 = number1 + number2
number1 = temp
def primes():
def isPrime(x):
if x == 1 or x == 2:
return True
elif x % 2 == 0:
return False
else:
d = x/2
while d > 1:
if d % 2 == 0:
return False
d -= 1
return True
num = 1
while True:
if isPrime(num) == True:
yield num
num += 1
def alphabet(code='', letters=''):
if code == "bg":
item = list("абвгдежзийклмнопрстуфхцчшщъьюя")
elif code == "lat":
item = list("abcdefghijklmnopqrstuvwxyz")
else:
item = letters
for letter in item:
yield letter
result = alphabet("bg")
def intertwined_sequences(*dict_arguments):
dictionaries = list(dict_arguments)
result = []
def helper(dict):
if dict['sequence'] == 'fibonacci':
result_item = fibonacci()
elif dict['sequence'] == 'primes':
result_item = primes()
elif dict['sequence'] == 'alphabet':
result_item = alphabet(dict['code'])
num = dict['length']
while num > 0:
result.append(next(result_item))
num -= 1
return result
for dict in dictionaries:
if (dict != ''):
yield helper(dict)

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

......EEEEEEEE
======================================================================
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_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

======================================================================
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

======================================================================
ERROR: test_primes (test.TestPrimes)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 65, in thread
    raise TimeoutError
TimeoutError

----------------------------------------------------------------------
Ran 14 tests in 16.131s

FAILED (errors=8)

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

Вера обнови решението на 03.04.2015 14:03 (преди около 9 години)

+def fibonacci():
+ counter = 0
+ while True:
+ if counter == 0 or counter == 1:
+ yield 1
+ counter += 1
+ else:
+ number1 = 1
+ number2 = 1
+ while True:
+ yield number1 + number2
+ temp = number2
+ number2 = number1 + number2
+ number1 = temp
+
+
+
+def primes():
+ def isPrime(x):
+ if x == 1 or x == 2:
+ return True
+ elif x % 2 == 0:
+ return False
+ else:
+ d = x/2
+ while d > 1:
+ if d % 2 == 0:
+ return False
+ d -= 1
+ return True
+ num = 1
+ while True:
+ if isPrime(num) == True:
+ yield num
+ num += 1
+
+
+def alphabet(code='', letters=''):
+ if code == "bg":
+ item = list("абвгдежзийклмнопрстуфхцчшщъьюя")
+ elif code == "lat":
+ item = list("abcdefghijklmnopqrstuvwxyz")
+ else:
+ item = letters
+ for letter in item:
+ yield letter
+result = alphabet("bg")
+
+
+def intertwined_sequences(*dict_arguments):
+ dictionaries = list(dict_arguments)
+ result = []
+
+ def helper(dict):
+ if dict['sequence'] == 'fibonacci':
+ result_item = fibonacci()
+ elif dict['sequence'] == 'primes':
+ result_item = primes()
+ elif dict['sequence'] == 'alphabet':
+ result_item = alphabet(dict['code'])
+ num = dict['length']
+ while num > 0:
+ result.append(next(result_item))
+ num -= 1
+ return result
+ for dict in dictionaries:
+ if (dict != ''):
+ yield helper(dict)