Posts: 7
Threads: 1
Joined: Jun 2019
I'm working on a code challenge in which we have to return the k non-prime +ve numbers.
The code comes with a locked generator code which is printing the all numbers also.
Here's the complete code:
from math import sqrt
def manipulate_generator(generator, n):
def is_prime(num):
for i in range(2,int(sqrt(num))+1):
if num % i == 0:
return False
return True
def yield_np(n):
np_counter = 1
num_to_print = 1
while np_counter <= n:
if num_to_print == 1 or is_prime(num_to_print) == False:
yield num_to_print
np_counter+=1
num_to_print+=1
if n > 0:
for i in yield_np(n):
yield i
else:
next(generator)
def positive_integers_generator():
n = 1
while True:
x = yield n
if x is not None:
n = x
else:
n += 1
k = int(input())
g = positive_integers_generator()
for _ in range(k):
n = next(g)
print(n)
manipulate_generator(g, n) For example, if I pass: 12 it's output should be:
1
4
6
8
9
10
12
14
15
16
18
20 but my output is:
1
2
3
4
5
6
7
8
9
10
11
12
Posts: 4,220
Threads: 97
Joined: Sep 2016
Jun-20-2019, 11:37 AM
(This post was last modified: Jun-20-2019, 11:37 AM by ichabod801.)
Your function manipulate_generator has a yield statement, so it is a generator. Therefore it returns an iterable. But you never iterate over that, so it never does anything.
However, it's not clear to me that it would ever manipulate the generator. The parameter n is always going to be > 0, since n comes from positive_integers_generator, so it never calls next(generator) on line 24.
If I understand correctly, you want to pass the first generator to a second generator so that only non-prime values are returned. So I would think the second generator would just pull a value from the first generator, check if the value is prime, and then yield that value if it isn't prime. Then you iterate over the second generator, not over range and not over the first generator.
Posts: 7
Threads: 1
Joined: Jun 2019
The positive_integers_generator is licked code from hackerrank, which we can 't edit, so should I remove the yields from manipulate_generator?
Posts: 4,220
Threads: 97
Joined: Sep 2016
I described two generators: first generator and second generator. First generator is positive_integers_generator (which I did not suggest changing) and second generator is manipulate_generator. So a translation of my last post would be: I would think manipulate_generator would just pull a value from the positive_integers_generator, check if the value is prime, and then yield that value if it isn't prime. Then you iterate over the manipulate_generator, not over range and not over the positive_integers_generator.
Posts: 7
Threads: 1
Joined: Jun 2019
If I try the code below:
def manipulate_generator(generator, n):
# Enter your code here
str1=[1]
for num in range(2,100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
str1.append(num)
break
if(len(str1)==n):
# for x in str1:
print(str1)
else:
None
pass it generates outoput like:
1
[1]
[1]
2
[1, 4]
[1, 4]
3
[1, 4, 6]
[1, 4, 6]
4
[1, 4, 6, 8]
5
[1, 4, 6, 8, 9]
6
[1, 4, 6, 8, 9, 10]
[1, 4, 6, 8, 9, 10]
7
[1, 4, 6, 8, 9, 10, 12]
[1, 4, 6, 8, 9, 10, 12]
8
[1, 4, 6, 8, 9, 10, 12, 14]
9
[1, 4, 6, 8, 9, 10, 12, 14, 15]
10
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16]
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16]
11
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]
12
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20] you have answered the similar question and suggest to skip the prime number by calling next(generator), but I don't know where should I call this next(generator) in manipulate_generator function?
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Jun-20-2019, 12:03 PM)arycloud Wrote: you have answered the similar question and suggest to skip the prime number by calling next(generator)
I did not suggest that. I did not suggest anything like that last bit of code you posted.
Read through what I suggested carefully. Try to write something that only does what I suggested.
Posts: 7
Threads: 1
Joined: Jun 2019
Jun-20-2019, 12:14 PM
(This post was last modified: Jun-20-2019, 12:14 PM by arycloud.)
I'm new to python, so need your help, please!
how should I adjust this code to get the required output:
def manipulate_generator(generator, n):
# Enter your code here
str1=[1]
for num in range(2,100):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
str1.append(num)
break
if(len(str1)==n):
# for x in str1:
print(str1)
else:
None
pass and we can't edit the code in positive_integers_generator.
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Jun-20-2019, 12:14 PM)arycloud Wrote: how should I adjust this code to get the required output:
You should delete it all and rewrite it as I suggested.
(Jun-20-2019, 12:14 PM)arycloud Wrote: and we can't edit the code in positive_integers_generator.
I got that part, which is why I have never suggested changing it.
Posts: 7
Threads: 1
Joined: Jun 2019
Jun-20-2019, 12:28 PM
(This post was last modified: Jun-20-2019, 12:29 PM by arycloud.)
Quote:You should delete it all and rewrite it as I suggested.
That's the thing I'm not clear about, can you little bit explain it via code? please!
Posts: 4,220
Threads: 97
Joined: Sep 2016
I'm not writing your code for you.
|