Generator Intermediate Python Prime | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

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? https://code.sololearn.com/ca248a106a20/?ref=app

10th May 2021, 1:11 PM
Lucas
Lucas - avatar
9 Answers
+ 13
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)))
12th May 2021, 8:27 AM
Josh Greig
Josh Greig - avatar
+ 3
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): for i in range(a, b): if isPrime(i): yield i i +=1 f = int(input()) t = int(input())
17th Aug 2022, 5:54 PM
Abdul Ghafar
Abdul Ghafar - avatar
0
Can you paste all the problem description including example inputs and outputs so non-Pro users like I can help?
11th May 2021, 7:48 PM
Josh Greig
Josh Greig - 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]
12th May 2021, 5:51 AM
Lucas
Lucas - avatar
0
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?
12th May 2021, 6:24 AM
Josh Greig
Josh Greig - avatar
0
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 https://code.sololearn.com/c5A1KLtixdq9/?ref=app Here is my latest approach
12th May 2021, 8:15 AM
Lucas
Lucas - avatar
0
Josh Grieg Thank You So Much I Struggled With This Problem So Much
12th May 2021, 1:40 PM
Lucas
Lucas - avatar
0
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)))
10th Nov 2022, 8:26 AM
Pooja Patel
Pooja Patel - avatar
0
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): #here was changed for x in range(t): if x >= f: if isPrime(x): yield x f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))
26th Nov 2023, 1:50 PM
vahid kamrani
vahid kamrani - avatar