D-OR Help needed

Please Help me to know the error in my code of October cookoff problem i.e. D-OR.
It doesn’t even pass the example in the problem.

def decimalToBinary(n,a):

if(n > 1):  
    a = decimalToBinary(n//2,a)  

a.append(n%2)
  
return a

t = int(input())
for _ in range(t):

l,r = map(int,input().split())

a = list()
c1 = list(decimalToBinary(r,a))


a = list()
c2 = list(decimalToBinary(l,a))
if(len(c2)!=len(c1)):               # If len(c1)>len(c2)
    c2.reverse()                    # Equating length
    while(len(c2)!=len(c1)):
        c2.append(0)
    c2.reverse()



ind = len(c1)-1
k = 0

while(k<len(c1)):
    if(c1[k] == c2[k]):
        k += 1
    else:
        ind = k
        break

for i in range(len(c1)-ind-1,-1,-1):
    c1[i] = 1

    
ans = 0
for i in range(len(c1)):
    if(c1[i]==1):
        ans += pow(2,i)
    else:
        ans += 0
        
print(ans)

It will be better for us to decode if u will also share your logic with your code.

1 Like

Post in original thread which can be found here.

1 Like

@shubh17cs @aryan12
Thanks For Your Help
I got my mistake…
I used the same approach as in thread by @aryan12. I have made some silly mistake in indices of for loop i.e.
for i in range(len(c1)-ind-1,-1,-1):
c1[i] = 1

which I have corrected to
for i in range(ind,len(c1)):
c1[i] = 1

and also in:
ans += pow(2,i)
which should be:
ans += pow(2,len(c1)-i-1)

also I have initialised variable ind to len(c1)-1
which should be len(c1)

Thanks again