Ad

How To Remove The Highest Number Of Consecutive Zeros In The String Python - No Imports

- 1 answer

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

Answer

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)
Ad
source: stackoverflow.com
Ad