Решение на Генератори от Димитър Антонов

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

Към профила на Димитър Антонов

Резултати

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

Код

def primes():
i=0
while 42==42:
i+=1
k=0
for j in range(i):
if i%(j+1)==0 and i!=(j+1) and (j+1)!=1:
k+=1
if k==0:
yield i
def semiprimes():
list=[]
sp=[]
for i in range(1000):
k=0
for j in range(i+1):
if (i+1)%(j+1)==0 and (i+1)!=(j+1) and (j+1)!=1:
k=k+1
if k==0:
list.append(i+1)
for n in list:
for p in list:
if list.index(n)>0 and list.index(p)>0:
sp.append(n*p)
sp.sort()
for i in range(len(sp)-1):
if sp[i]!=sp[i+1]:
yield (sp[i])

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

F.F.
======================================================================
FAIL: test_first_few_primes (__main__.PrimesTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/tmp/test20120627-11891-14sl7jx", line 2014, in test_first_few_primes
    self.assertEqual(ten_thousand_primes[i], next(primes))
AssertionError: 2 != 1

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

----------------------------------------------------------------------
Ran 4 tests in 0.821s

FAILED (failures=2)

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

Димитър обнови решението на 03.05.2012 17:12 (преди над 8 години)

+def primes():
+ i=0
+ while 42==42:
+ i+=1
+ k=0
+ for j in range(i):
+ if i%(j+1)==0 and i!=(j+1) and (j+1)!=1:
+ k+=1
+ if k==0:
+ yield i
+
+
+
+def semiprimes():
+ list=[]
+ sp=[]
+ for i in range(1000):
+ k=0
+ for j in range(i+1):
+ if (i+1)%(j+1)==0 and (i+1)!=(j+1) and (j+1)!=1:
+ k=k+1
+ if k==0:
+ list.append(i+1)
+ for n in list:
+ for p in list:
+ if list.index(n)>0 and list.index(p)>0:
+ sp.append(n*p)
+ sp.sort()
+ for i in range(len(sp)-1):
+ if sp[i]!=sp[i+1]:
+ yield (sp[i])