Generator Intermediate Python Prime | Sololearn: Learn to code for FREE!


Generator Intermediate Python Prime

How do you solve this question? I tried several methods, normal, dictionary based, etc..., but none got me to solve this. I’ve been stuck on it and I cannot progress Any ideas?

5/10/2021 1:11:43 PM


8 Answers

New Answer


How many test cases does it pass with the interval exclusive at the upper end like this? There could be some subtle assumption we're adding to the requirements that just doesn't fit their test cases. The 10 to 20 and 5 to 18 intervals end with numbers that are not prime so it is ambiguous whether the interval's upper boundary is to be included. Try this: def isPrime(x): if x < 2: return False else: for n in range(2, x): if x % n == 0: return False return True def primeGenerator(a, b): for i in range(a, b ): # <~~~~~ HERE is the CHANGE if isPrime(i): yield i f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))


Don't see answers try it your selfπŸ˜πŸ˜πŸ‘πŸ‘


Can you paste all the problem description including example inputs and outputs so non-Pro users like I can help?


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]


Simon shared the problem description. Response: The question says "primeGenerator" but your code mentions "primeGenerator" nowhere. Maybe renaming "factory" to "primeGenerator" will help. This is trivial and not what you're asking about but "from_to = [i for i in range(f, t + 1)]" can be simplified to "from_to = range(f, t + 1)". That change will shorten your code while maintaining the current behaviour. Another thing I notice is you're creating a decorator and a generator when the question asks only for a generator. Yet another point is the question asks for a function taking 2 arguments and your functions only take 1. Try this: f = int(input()) t = int(input()) def primeGenerator(f, t): for i in range(f, t + 1): if is_prime(i): yield i def is_prime(nums): if nums <= 1: return False else: for num in range(2, nums): if nums % num == 0: return False return True prime_list = list(primeGenerator(f, t)) print(prime_list) If the above suggestions don't cause the test cases to pass, can you also share the visible test cases? What are their inputs, expected outputs, and what error messages do you get, if any?


The first thing you get are the nums 5 & 18. Like my code, it only does the first 3 and fails at the 4 question. The only visible one is test 1 where 5 and 18 are input Here is my latest approach


You can combine generator and isPrime this way: a, b = 0, 71 def isPrime(n): if n <= 1: return for k in range (2, int(n**0.5)+1): if not n % k: return yield p = [n for n in range(a, b+1) for bar in isPrime(n)] print(p)


Josh Grieg Thank You So Much I Struggled With This Problem So Much