0
l=['hello','world']
first=l[0]
second=l[1]
d=len(first); 
x=0
while d>=0:
    e=len(second)-1
    while e>=0:
        if first[d-1]==second[e]:
            x+=1
        else:
            e-=1
    d-=1
if x>0:
    print("YES")
else:
    print("NO")

I am working on python 3. The above code should print YES as output but it isn't and rather program keeps on running indefinitely. I am new to python. Is there something I am missing.

1 Answer 1

1

You should change your while loop to

while e>=0:
    if first[d-1]==second[e]:
        x+=1
    e-=1

Reason for the same is, say first[d-1] = 'a' and second[e] also = 'a' then it will never decrement value of e and you while condition e>=0 will satify and it will again goto if condition as d hasn't changed nor the e variable and hence lead to infinite loop.

Sign up to request clarification or add additional context in comments.

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.