0

I have the following function which I used to find the length of the places in a binary number where zeros are encapsulated by ones:

def solution(N):
# write your code in Python 2.7
# convert to binary
binary = bin(N)[2:] # results prefixed with '0b'
# special case of no zeros
if '0' not in str(binary):
    print('No binary gap in %s'% str(binary))
    return 0
# special case of all zeros after first 1
if '1' not in str(binary)[1:]:
    print('No binary gap in %s'% str(binary))
    return 0
# special case of N == 1 = 01
if N == 1:
    print('No binary gap in %s'% str(binary))
    return 0

bgaps = []
sbin = str(binary)
print(sbin)
spbin = sbin.split('1')
print(spbin)
for i in spbin:
    if i == '': continue
    bgaps.append(len(i))

return max(bgaps)

for N in [6,328,147,15,2,483,647]:
    print(solution(N)

The results show that string split doesn't always return a '' where the delimiter used to be. The happens e.g. for 101001000 where the split returns

['', '0', '00', '000']

instead of

['', '0','', '00', '', '000']

I suspect that this has to do with a special meaning of '01' but the delimiter is '1'. Any thoughts on why this split behaves this way?

2
  • 1
    this is not 2.7 Commented Aug 10, 2017 at 21:04
  • 1
    split throws away the delimiter: "a,b,c".split(',') == ["a", "b", "c"]. Commented Aug 10, 2017 at 21:06

3 Answers 3

4

You seem to misunderstand how str.split works. Since 1 is at the start of the string, the empty string appears on the left side of the split at the start character; str.split puts this into consdiration:

>>> '1'.split('1')
['', '']

Looks like you don't want to split.

You probably want something of the sort:

>>> from itertools import groupby
>>> sbin = '101001000'
>>> ['' if k=='1' else ''.join(g)  for k, g in groupby(sbin)]
['', '0', '', '00', '', '000']
Sign up to request clarification or add additional context in comments.

3 Comments

I was going to post a solution with groupby too, but it appears as if the OP is doing homework, in which case using something like gropuby would probably be considered cheating. Maybe provide a hand-rolled solution as well?
@ChristianDean I added that since they've shown some effort at working the larger part 'alone' :)
@ Moses Koledoye Appreciate the clarification. This question is not "homework" just learning python via the "Codility" and GeeksforGeeks websites.
2

Update Added exception if last element is a 1.

How about replacing the "1" with "11" adding an empty space between.

sbin = '10100100'

if sbin[-1] == "1":
    print(sbin.replace("1","11").split("1")[1:-1]) # remove first and last
else:
    print(sbin.replace("1","11").split("1")[1:]) # remove first

prints

['', '0', '', '00', '', '000']

1 Comment

An interesting solution :-)
2

As @ForceBru mentioned, split removes the delimiter which is the same behavior in a lot of languages. You probably want to approach this differently (perhaps using a combination of split + replace or something similar to count the gaps).

Hope that provides some insight.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.