Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in python finding coolness of the number

coolness of the number is defined as the number of "101"s occuring in its binary representation. If the number binary representation has "101" then it’s coolness is 1
A number is defined as very cool if it’s coolness is greater than or equal to k

input : 21 2 output: 1

but i’m getting

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

output:2 instead of 1

 def v(j):
       res=''
        c=0
      while j>0:
        res=res+str(j%2)
        j=j//2
        for i in range(2,len(res)):
            if res[i]+res[i-1]+res[i-2]=="101":
                c+=1
    return c>=k
n,k=map(int,input().split())
r=0
for i in range(2,n+1):
    if v(i):
        r=r+1
print(r)

>Solution :

while j>0:
    res=res+str(j%2)
    j=j//2
for i in range(2,len(res)):
    if res[i]+res[i-1]+res[i-2]=="101":
        c+=1
return c>=k
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading