Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first k non-prime numbers
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
The positive_integers_generator is licked code from hackerrank, which we can 't edit, so should I remove the yields from manipulate_generator?
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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?
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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.
Reply
#8
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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!
Reply
#10
I'm not writing your code for you.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,772 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  prime numbers astral_travel 28 3,472 Nov-08-2022, 09:23 PM
Last Post: astral_travel
  Return prime numbers from range krzyfigh 2 1,886 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Prime numbers Anderi02 1 1,941 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non prime numbers print bsrohith 7 7,424 Jun-20-2019, 10:48 AM
Last Post: arycloud
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,667 May-09-2019, 12:19 PM
Last Post: Pleiades
  Finding prime numbers creslin_black 7 4,301 Jul-20-2018, 02:28 PM
Last Post: grjmmr
  Prime Numbers OmarSinno 1 4,351 Sep-23-2017, 05:29 PM
Last Post: ichabod801
  prime numbers generator is generating non prime numbers? ixaM 2 4,418 Dec-18-2016, 01:35 PM
Last Post: ixaM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020