+ 1

# Generators in python

Hey there dear programmers, I need assistance with my code. It passes 4 out 5 test cases and i do not clearly understand why. here is the scenario question: 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] here is the 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 prime in range(a, b + 1): if isPrime(prime): yield prime f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))

7th Nov 2023, 8:27 AM
Blessing Malema
6 ответов
+ 1
...output the prime numbers in the given RANGE (between the two arguments)... I also failed at my first run because I though t is inclusive. You can 'hard code' it and test if t is included on other cases.
7th Nov 2023, 1:27 PM
Wong Hei Ming
+ 1
All of your lines are correct except line 13
7th Nov 2023, 11:37 PM
Annihilate
+ 1
Blessing Malema , You have a one-off error. Change this, for prime in range(a, b + 1): to this. for prime in range(a, b): The Sololearn description is ambiguous and catches a lot of people. Instead of "between the two arguments", they should have written "not less than the first argument and less than the second argument." The locked test case that catches people must be one where the second input is a prime number but is not expected to be included in the output.
8th Nov 2023, 7:05 AM
Rain
0
Blessing Malema just use for prime in range(a, b): it does not include b
7th Nov 2023, 12:16 PM
Bob_Li
0
Blessing Malema , at first sight, it seems to me, that you check twice for x == 2 case - first in the elif clause, second in the loop (it starts again with 2, instead of 3).
7th Nov 2023, 12:18 PM
TheWh¡teCat 🇧🇬
0
The issue with your code might be related to the `isPrime()` function. The loop in `isPrime()` should only iterate up to the square root of `x` to check for factors efficiently. Currently, it iterates up to `x-1`, which is not necessary and makes the function less optimized. Here's an updated version of the `isPrime()` function: ```python import math def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, int(math.sqrt(x)) + 1): if x % n == 0: return False return True ``` By using `int(math.sqrt(x)) + 1` as the upper limit of the loop, you only need to check divisibility up to the square root of `x`, which significantly improves the efficiency of the prime-checking algorithm. Try replacing your original `isPrime()` function with this updated version and see if it improves the accuracy of your prime generator.
9th Nov 2023, 3:45 AM
Learnji
Актуальное сегодня
HELP ME