0

Generators on Python.Pls help

Good afternoon or evening. I have a problem with the Generators. In general, I understand how to output the desired values. But I can't pack them into a list. Help please. Here is the task and my code: Task: 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] 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): for i in range(a, b): if isPrime(i) is True: print(i) f = int(input()) t = int(input()) print(list(primeGenerator(f, t))) Thanks

11th Jun 2021, 10:54 AM
Andrey Zinin
10 Réponses
+ 3
Andrey Zinin , it looks like you have not worked through the tutorial in lesson 14.1. most of the things that you need to solve the exercise are explained in this part of the tutorial. please repeat some steps and then try to solve it again. happy coding and good success!
11th Jun 2021, 11:00 AM
Lothar
Lothar - avatar
+ 2
okey i do it. Sorry guys and thank you again It work)
11th Jun 2021, 2:20 PM
Andrey Zinin
+ 1
Sorry, but i don't understand how i can use yield in this task and how can append values i in list....
11th Jun 2021, 2:10 PM
Andrey Zinin
+ 1
i really read all previous lesson, but don't understand yet
11th Jun 2021, 2:11 PM
Andrey Zinin
+ 1
'yield' keyword is required in a generator function... else that's just a legacy function ;P 'yield' is the partial return instruction of a generator: that's what make the function acting as an iterable: 'yield' return a value while pausing execution until next call... that's why to print the list you must to explicitly convert the 'iterable' generator to a list so the generator is entirely consumed and all values appended to a new list... alternatively, you could iterate over the generator: for prime in primeGenerator(int(input()),int(input())): print(prime) or even unpack all the generator values in print() arguments: print(*(primeGenerator(int(input()),int(input())))) however, the task expect to have the list representation as output, so only the: print(list(primeGenerator(int(input()),int(input())))) will works until you doesn't mimic the list representation output by adding some stuff to alternatives ^^
11th Jun 2021, 4:35 PM
visph
visph - avatar
+ 1
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): l=[] for i in range(a, b): if isPrime(i) is True: l.append(i) return l f = int(input()) t = int(input()) print(primeGenerator(f, t)) This would be useful!
13th Jun 2021, 8:22 AM
Abhinandan Prasad
0
Ok, i will try. Thanks
11th Jun 2021, 11:10 AM
Andrey Zinin
0
Andrey Zinin Append i in a list instead of printing i . Then print that list. You can also use yield but for this you need to learn previous tutorial.
11th Jun 2021, 12:54 PM
A͢J
A͢J - avatar
0
@Abhinandan Prasad thx!)
11th Nov 2023, 7:03 AM
SyntaxSlayer
SyntaxSlayer - avatar
0
def isPrime(x): if x < 2: return False elif x == 2: return True for n in range(2, int(x ** 0.5) + 1): if x % n == 0: return False return True def primeGenerator(a, b): for i in range(a, b): if isPrime(i) is True: yield i f = int(input()) t = int(input()) print(list(primeGenerator(f, t)))
8th Apr 2024, 9:52 AM
Rashmi Kulkarni