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

How to remove the highest number of consecutive zeros in the string Python – no imports

What I would like to do is read in a binary string consisting of only zeros and ones.
Something like that:

input
1001101000110

I then need to find the highest number of only zeros

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

1001101000110 –> biggest number of consecutive zeros
remove the zeros
1001101110

And then also ouput the result as a decimal number so

output
622

I can already transform it into a decimal but I don’t know how to find the highest zero number..
I only have the function to transform it into a decimal number.

def binaryToDecimal(n):
return int(n,2)


input = str(input(binaryToDecimal('1001101000110')))

>Solution :

I think this solves the first part of your question;

s = '1001101000110'

def remover(s):
  res = s.split('1')
  m = max(res)
  newstr = s.replace(m, '')
  return newstr 

res = remover(s)
print(res)
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