Generators isPrime | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Generators isPrime

Hi all, I am stuck on this problem. I pass all the test casey but the last one. I thought it was a problem of order of the input values but still I don't understand what to change. Any ideas? Below is my code: def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, x): if x % n ==0: return False return True def primeGenerator(a, b): #your code goes here for i in range(a,b+1): if isPrime (i)== True: yield i for i in range (b, a+1): if isPrime (i) == True: yield i f = int(input()) t = int(input()) print(list(primeGenerator(f, t))) Cheers

11th May 2021, 7:52 PM
Brassac
Brassac - avatar
14 Answers
+ 2
Brassac The only other special case I can think of right now is when the bounds themselves are prime numbers. So I don't know if "between the two arguments" includes the arguments or not. So for 4, 7 should the program output 7 or not? Same for the lower bound. You should try if changing the handling of the arguments solves the last test case.
14th May 2021, 2:48 PM
Hape
Hape - avatar
+ 2
Hi Calvin Thomas I guess my question title was misleading, the problem is not in the isPrime function which is already given but in the primeGenerator function. Anyhow thanks for the suggestion, I guess it's always a good thing to read alternative ways to code.
12th May 2021, 8:22 AM
Brassac
Brassac - avatar
+ 1
Brassac If you want to fix the problem just remove the second for loop and change the first range to range(min(a,b), max(a,b)+1)
12th May 2021, 12:35 PM
Hape
Hape - avatar
0
In primeGenerator if a == b and a is prime you yield it twice. For example primeGenerator (3,3) will yield 3 twice.
11th May 2021, 11:13 PM
Hape
Hape - avatar
0
Here's a simpler version of your is_prime function: def is_prime(x): if x == 2: return True if x < 2 or x % 2 < 1: return False return all(x % i for i in range(3, x, 2)) # Hope this helps
12th May 2021, 2:33 AM
Calvin Thomas
Calvin Thomas - avatar
0
Hi Hape sorry but I am not sure to understand why if a==b and a is prime, the function should yield a twice because the function anyway makes b+1.
12th May 2021, 7:13 AM
Brassac
Brassac - avatar
0
Brassac The first for loop iterates i from a to b inclusive and the second one from b to a inclusive. I think you were trying to compensate for the fact that you don't know which of a and b is greater. But this leads in the special case of a==b to both for loops beginning and ending with that number so that both loops encounter a. And if a is a prime both loops will yield it.
12th May 2021, 8:50 AM
Hape
Hape - avatar
0
Brassac I could try to help, if you could explain what the generator function should do.
12th May 2021, 12:21 PM
Calvin Thomas
Calvin Thomas - avatar
0
Hape that's a nice way to do it (I had tried if, elif statements that achieved the same thing but way longer) but still I am missing something because the last test case is still wrong.
12th May 2021, 1:17 PM
Brassac
Brassac - avatar
0
Brassac could you please share the exact code you have right now? I can't think of anything else that could be wrong right now. It would also be helpful if you could write what the exact definition of the task you are trying to solve is.
12th May 2021, 1:35 PM
Hape
Hape - avatar
0
Finding prime numbers is a common coding interview task. The given code defines a function isPrime(x), which returns True if x is prime. You need to create a generator function primeGenerator(), that will take two numbers as arguments, and use the isPrime() function to output the prime numbers in the given range (between the two arguments). Sample Input: 10 20 Sample Output: [11, 13, 17, 19] And here is my current code. As already mentioned, it solves all test cases but the last one which is hidden. I still don't understand what special case this could be. https://code.sololearn.com/cq4y6LvEo4l1/?ref=app
14th May 2021, 1:25 PM
Brassac
Brassac - avatar
0
Hape thanks for the suggestion, that solved the problem. I guess that everything was mentioned in the comment "between the two arguments".
14th May 2021, 2:56 PM
Brassac
Brassac - avatar
14th May 2021, 4:33 PM
Calvin Thomas
Calvin Thomas - avatar
0
Simply remove the one you are adding to the other input def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, x): if x % n ==0: return False return True def primeGenerator(a, b): #your code goes here for i in range(a,b): if isPrime (i)== True: yield i for i in range (b, a): if isPrime (i) == True: yield i f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))
28th Aug 2023, 11:49 AM
Ajibodu Enoch
Ajibodu Enoch - avatar