Решение на Генератори и итератори от Илиан Стаменов

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

Към профила на Илиан Стаменов

Резултати

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

Код

import string
from itertools import islice
class fibonacci():
def __init__(self):
self.current = 1
self.previous = 0
def __iter__(self):
self.current = 1
self.previous = 0
return self
def __next__(self):
return_index = self.current
self.current, self.previous = self.previous + self.current, return_index
return return_index
class primes():
def primality_check(self, number):
for i in range(2,number):
if number % i == 0:
return False
return True
def __init__(self):
self.current = 2
def __iter__(self):
self.current = 2
return self
def __next__(self):
while (not self.primality_check(self.current)):
self.current = self.current + 1
result, self.current = self.current, self.current + 1
return result
class alphabet():
def __init__(self, code, letters=''):
self.current = 0
if letters != '':
self.letters = letters
elif code == 'bg':
self.letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
elif code == 'lat':
self.letters = string.ascii_lowercase
def __iter__(self):
self.__init__('lat')
return self
def __next__(self):
result, self.current = self.current, self.current + 1
return self.letters[result]

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

.EE...EEEEEE..
======================================================================
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_glagolitic (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_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 17.513s

FAILED (errors=8)

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

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

+import string
+from itertools import islice
+
+
+class fibonacci():
+ def __init__(self):
+ self.current = 1
+ self.previous = 0
+
+ def __iter__(self):
+ self.current = 1
+ self.previous = 0
+ return self
+
+ def __next__(self):
+ return_index = self.current
+ self.current, self.previous = self.previous + self.current, return_index
+ return return_index
+
+
+class primes():
+ def primality_check(self, number):
+ for i in range(2,number):
+ if number % i == 0:
+ return False
+ return True
+
+ def __init__(self):
+ self.current = 2
+
+ def __iter__(self):
+ self.current = 2
+ return self
+
+ def __next__(self):
+ while (not self.primality_check(self.current)):
+ self.current = self.current + 1
+ result, self.current = self.current, self.current + 1
+ return result
+
+class alphabet():
+ def __init__(self, code, letters=''):
+ self.current = 0
+ if letters != '':
+ self.letters = letters
+ elif code == 'bg':
+ self.letters = 'абвгдежзийклмнопрстуфхцчшщъьюя'
+ elif code == 'lat':
+ self.letters = string.ascii_lowercase
+
+ def __iter__(self):
+ self.__init__('lat')
+ return self
+
+ def __next__(self):
+ result, self.current = self.current, self.current + 1
+ return self.letters[result]