Решение на Генератори от Иво Кънчев

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

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

Резултати

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

Код

def primes():
D = {}
number = 2
while True:
if number not in D:
yield number
D[number * number] = [number]
else:
for prime in D[number]:
D.setdefault(prime + number, []).append(prime)
del D[number]
number += 1
def get_prime(prime_index):
primes = []
prime_generator = primes()
if prime_index == 0:
return 2
if ( prime_index > len(primes) ):
for i in range(len(primes), prime_index):
primes.append(next(prime_generator))
return primes[prime_index -1]
def semiprimes():
pass # :D
def counting_sort(array, maxval):
n = len(array)
m = maxval + 1
count = [0] * m
for a in array:
count[a] += 1
i = 0
for a in range(e):
for c in range(count[a]):
array[i] = a
i += 1
return array

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

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

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

----------------------------------------------------------------------
Ran 4 tests in 0.005s

FAILED (errors=2)

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

Иво обнови решението на 03.05.2012 17:07 (преди над 8 години)

+def primes():
+ D = {}
+ number = 2
+ while True:
+ if number not in D:
+ yield number
+ D[number * number] = [number]
+ else:
+
+ for prime in D[number]:
+ D.setdefault(prime + number, []).append(prime)
+ del D[number]
+ number += 1
+
+def get_prime(prime_index):
+ primes = []
+ prime_generator = primes()
+ if prime_index == 0:
+ return 2
+ if ( prime_index > len(primes) ):
+ for i in range(len(primes), prime_index):
+ primes.append(next(prime_generator))
+ return primes[prime_index -1]
+
+
+def semiprimes():
+ pass # :D
+
+def counting_sort(array, maxval):
+ n = len(array)
+ m = maxval + 1
+ count = [0] * m
+ for a in array:
+ count[a] += 1
+ i = 0
+ for a in range(e):
+ for c in range(count[a]):
+ array[i] = a
+ i += 1
+ return array