Home > front end >  How to remove the highest number of consecutive zeros in the string Python - no imports
How to remove the highest number of consecutive zeros in the string Python - no imports

Time:03-18

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

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')))

CodePudding user response:

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)
  • Related