Reputation: 126
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.
Upvotes: 0
Views: 870
Reputation: 37023
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.
Upvotes: 1