Answer to Question #349401 in Python for Bhavani

Question #349401

Nicholas is trying to invent a light bulb. he has to discover the right gaseous mixture that can be used to fill the bulb so that the bulb works underwater. He has prepared N gaseous mixture numbered from 0 to n-1 to check . Nicholas does not know which this gases will work, but to use a modulo-based search algorithm to keep checking different gas mixtures. So 1st chooses an integer K and select all indices i in increasing order such that i mod k= 0 (i%K==0) and tests the gases on such indices, then all indices in increasing order such that i mod k=1 and test the gases on such indices and so on.

Input:Test cases

test case contains N, P and K

output:no.of days taken

N=10 P=6 K=5

On the day1,Nicholas Will test gas numbered 0 as 0 mod 5=0,

On the day2, Nicholas Will test gas numbered 5 as 5 mod 5=0,

On the day3, Nicholas Will test gas numbered 1 as 1 mod 5=1,

On the day4, Nicholas Will test gas numbered 6 as 6 mod 5=1,

Nicolas test the gas number 6 on day 4 so the output is 4

Input:

10 6 5

5 2 3

Output:

4

5


1
Expert's answer
2022-06-10T08:30:03-0400
import sys
import itertools

_, *args = sys.argv
N, P, K = [int(str) for str in args]

count = 0
for n in itertools.count():
    for i in range(0, N):
        if i % K == n:
            count += 1
            if i == P:
                print(count)
                sys.exit()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS