A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N.
For example, number 9 has binary representation 1001 and contains a binary gap of length 2. The number 529 has binary representation 1000010001 and contains two binary gaps: one of length 4 and one of length 3. The number 20 has binary representation 10100 and contains one binary gap of length 1. The number 15 has binary representation 1111 and has no binary gaps. The number 32 has binary representation 100000 and has no binary gaps.
Write a function:
def solution(N)
that, given a positive integer N, returns the length of its longest binary gap. The function should return 0 if N doesn't contain a binary gap.
For example, given N = 1041 the function should return 5, because N has binary representation 10000010001 and so its longest binary gap is of length 5. Given N = 32 the function should return 0, because N has binary representation '100000' and thus no binary gaps.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..2,147,483,647].
example test n=1041=10000010001_2
tested program terminated with exit code 1
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=list(N)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 5, in solution a=list(N) TypeError: 'int' object is not iterable
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 5, in solution a=list(N) TypeError: 'int' object is not iterable
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 5, in solution a=list(N) TypeError: 'int' object is not iterable
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(1041, 'b')
a=list(N)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(N)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 6, in solution a=list(N) TypeError: 'int' object is not iterable
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 6, in solution a=list(N) TypeError: 'int' object is not iterable
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 6, in solution a=list(N) TypeError: 'int' object is not iterable
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(int(N), 'b')
a=list(N)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(int(N), 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution return answer UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution return answer UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
if answer='1'
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
if answer==1:
answer=0
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
if answer==1:
answer=0
re.clear()
return answer
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 22, in solution return answer UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
if answer==1:
answer=0
return answer
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution if answer==1: UnboundLocalError: local variable 'answer' referenced before assignment
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution if answer==1: UnboundLocalError: local variable 'answer' referenced before assignment
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution if answer==1: UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution return answer UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
answer
example test n=1041=10000010001_2
tested program terminated with exit code 1
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 20, in solution answer UnboundLocalError: local variable 'answer' referenced before assignment
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
ans
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
answer
example test n=1041=10000010001_2
tested program terminated with exit code 1
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
Invalid result type, int expected, <class 'NoneType'> found.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return answer
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
answer=0
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
answer=int('0b'+''.join(re),2)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
re.clear()
if
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in ('0'):
length==0
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in ('0'):
length==0
lenght=len(re)
re.clear()
return lenght
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in ('0'): TypeError: 'in <string>' requires string as left operand, not list
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in ('0'): TypeError: 'in <string>' requires string as left operand, not list
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in (0):
length==0
lenght=len(re)
re.clear()
return lenght
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in (0): TypeError: argument of type 'int' is not iterable
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in (0): TypeError: argument of type 'int' is not iterable
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in ('0'):
length==0
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if re not in ('0'):
length==0
lenght=len(re)
re.clear()
return lenght
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in ('0'): TypeError: 'in <string>' requires string as left operand, not list
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 16, in solution if re not in ('0'): TypeError: 'in <string>' requires string as left operand, not list
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if ('0'):
length==0
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if '0' not in re:
length==0
lenght=len(re)
re.clear()
return lenght
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 85, in main result = solution( N ) File "/tmp/solution.py", line 17, in solution length==0 NameError: name 'length' is not defined
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if '0' not in re:
length=0
lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
else :
if len(re)>lenght:
if '0' not in re:
length=0
else lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if i==len(a)-1 or a[i]=='1':
if len(re)>lenght:
if '0' not in re:
length=0
else : lenght=len(re)
re.clear()
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if i==len(a)-1 or a[i]=='1':
if len(re)>lenght:
if '0' not in re:
length=0
else : lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if i==len(a)-1 or a[i]=='1':
if len(re)>lenght:
if '0' not in re:
length=0
else : lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
re.append(a[i])
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
re.append(a[i])
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
re.append(a[i])
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
re.append(a[i])
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
example test n=1041=10000010001_2
tested program terminated with exit code 1
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 16 lenght=len(re) ^ IndentationError: expected an indented block
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 16 lenght=len(re) ^ IndentationError: expected an indented block
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 16 lenght=len(re) ^ IndentationError: expected an indented block
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
re.append(a[i])
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(N):
a=format(N, 'b')
a=list(a)
lenght=0
re=[]
for i in range(len(a)):
if len(re)==0 and a[i]=='1':
re.append(a[i])
elif a[i]=='0':
re.append(a[i])
if a[i]=='1':
if len(re)>lenght:
lenght=len(re)
re.clear()
if a.count('1')==1 or a.count('0')==0:
lenght=0
return lenght
The following issues have been detected: wrong answers.
For example, for the input 6 the solution returned a wrong answer (got 1 expected 0).