Решение на Генератори от Василина Татарлиева

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

Към профила на Василина Татарлиева

Резултати

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

Код

import re
class primes(object):
def __init__(self):
self.prime = 2
def __iter__(self):
return self
def next( self ):
regex_str = r'^1?$|^(11+?)\1+$'
expression = re.compile( regex_str )
if self.prime is 2:
self.prime = self.prime + 1
return 2
else:
while re.search( expression , '1'*self.prime ):
self.prime = self.prime + 1
self.prime = self.prime + 1
return self.prime - 1
class semiprimes(object):
def __init__(self):
self.semiprime = 2*2
regex_str = r'^1?$|^(11+?)\1+$'
self.expression = re.compile( regex_str )
def __iter__(self):
return self
def primefactors( self ):
mockprime = self.semiprime
factorlist = 0
p = primes()
loop = next(p)
while loop <= mockprime:
if mockprime % loop == 0:
mockprime /= loop
factorlist += 1
if factorlist > 2:
return False
else:
loop = next(p)
if factorlist == 2:
return True
else:
return False
def next( self ):
if self.semiprime == 4:
self.semiprime += 1
return 4
else:
while self.primefactors() is False:
self.semiprime += 1
self.semiprime += 1
return self.semiprime - 1

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

EEEE
======================================================================
ERROR: test_first_few_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1knfpqb", line 2014, in test_first_few_primes
    self.assertEqual(ten_thousand_primes[i], next(primes))
TypeError: primes object is not an iterator

======================================================================
ERROR: test_first_few_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1knfpqb", line 2019, in test_first_few_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
TypeError: semiprimes object is not an iterator

======================================================================
ERROR: test_thousands_of_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1knfpqb", line 2024, in test_thousands_of_primes
    self.assertEqual(ten_thousand_primes[i], next(primes))
TypeError: primes object is not an iterator

======================================================================
ERROR: test_thousands_of_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-1knfpqb", line 2029, in test_thousands_of_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
TypeError: semiprimes object is not an iterator

----------------------------------------------------------------------
Ran 4 tests in 0.004s

FAILED (errors=4)

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

Василина обнови решението на 03.05.2012 09:02 (преди над 8 години)

+import re
+class primes(object):
+
+ def __init__(self):
+ self.prime = 2
+ def __iter__(self):
+ return self
+ def next( self ):
+ regex_str = r'^1?$|^(11+?)\1+$'
+ expression = re.compile( regex_str )
+ if self.prime is 2:
+ self.prime = self.prime + 1
+ return 2
+ else:
+ while re.search( expression , '1'*self.prime ):
+ self.prime = self.prime + 1
+ self.prime = self.prime + 1
+ return self.prime - 1
+
+class semiprimes(object):
+ def __init__(self):
+ self.semiprime = 2*2
+ regex_str = r'^1?$|^(11+?)\1+$'
+ self.expression = re.compile( regex_str )
+ def __iter__(self):
+ return self
+ def primefactors( self ):
+ mockprime = self.semiprime
+ factorlist = 0
+ p = primes()
+ loop = next(p)
+ while loop <= mockprime:
+ if mockprime % loop == 0:
+ mockprime /= loop
+ factorlist += 1
+ if factorlist > 2:
+ return False
+ else:
+ loop = next(p)
+ if factorlist == 2:
+ return True
+ else:
+ return False
+
+ def next( self ):
+ if self.semiprime == 4:
+ self.semiprime += 1
+ return 4
+ else:
+ while self.primefactors() is False:
+ self.semiprime += 1
+ self.semiprime += 1
+ return self.semiprime - 1