Решение на Генератори от Албена Куманова

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

Към профила на Албена Куманова

Резултати

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

Код

primeNumbers = []
semiPrimes = []
var = currentStart = None
def primes():
if not primeNumbers:
primeNumbers.append(2)
yield 2
if len(primeNumbers) == 1:
primeNumbers.append(3)
yield 3
for m in myrange(3, 2):
if all([m % p for p in primeNumbers]):
primeNumbers.append(m)
yield m
def semiprimes():
if not semiPrimes:
semiPrimes.append(4)
yield 4
for m in myrange(5, 1):
if m in semiPrimes:
continue
if not primeNumbers:
next(primes())
while primeNumbers[len(primeNumbers) - 1] < m / 2:
next(primes())
for prime in primeNumbers:
if m in map(lambda x: x * prime, primeNumbers):
semiPrimes.append(m)
yield m
break
def myrange(start, step):
global currentStart, var
if start != currentStart:
var = start
currentStart = start
while(True):
var += step
yield var - step

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

..FF
======================================================================
FAIL: test_thousands_of_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-i47rg6", line 2024, in test_thousands_of_primes
    self.assertEqual(ten_thousand_primes[i], next(primes))
AssertionError: 2 != 233

======================================================================
FAIL: test_thousands_of_semi_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-i47rg6", line 2029, in test_thousands_of_semi_primes
    self.assertEqual(ten_thousand_semiprimes[i], next(primes))
AssertionError: 4 != 155

----------------------------------------------------------------------
Ran 4 tests in 0.198s

FAILED (failures=2)

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

Албена обнови решението на 03.05.2012 09:56 (преди над 12 години)

+primeNumbers = []
+semiPrimes = []
+var = currentStart = None
+
+
+def primes():
+ if not primeNumbers:
+ primeNumbers.append(2)
+ yield 2
+ if len(primeNumbers) == 1:
+ primeNumbers.append(3)
+ yield 3
+
+ for m in myrange(3, 2):
+ if all([m % p for p in primeNumbers]):
+ primeNumbers.append(m)
+ yield m
+
+
+def semiprimes():
+ if not semiPrimes:
+ semiPrimes.append(4)
+ yield 4
+
+ for m in myrange(5, 1):
+ if m in semiPrimes:
+ continue
+ if not primeNumbers:
+ next(primes())
+ while primeNumbers[len(primeNumbers) - 1] < m / 2:
+ next(primes())
+ for prime in primeNumbers:
+ if m in map(lambda x: x * prime, primeNumbers):
+ semiPrimes.append(m)
+ yield m
+ break
+
+
+def myrange(start, step):
+ global currentStart, var
+ if start != currentStart:
+ var = start
+ currentStart = start
+
+ while(True):
+ var += step
+ yield var - step