Reputation: 5509
I was trying to solve this Question in HackerRank.
#t=int(input().strip())
t=1
for i in range(0,t):
#u=input()
#pw=str(input()).split(' ')
#s=input().strip()
u=6
pw=['because', 'can','do', 'must', 'we', 'what']
s="wedowhatwemustbecausewecan"
pw_in_s=[]
for p in pw:
if p in s:
pw_in_s.append(p)
print(pw_in_s)
start=0
length=1
res=""
while start+length<=len(s):
tmp=s[start:start+length]
print (tmp)
if tmp in pw_in_s:
res+=" "+tmp
start=length
length=1
else:
length=length+1
print(res)
The code is not complete to solve the question. But I'm stuck in halfway.
Problem
Even though the list pw_in_s
contains an item 'do'
, the if tmp in pw_in_s
is not getting satisfied when tmp
is 'do'
. Also the program runs into an infinite loop, because length
value is not getting incremented.
Where is the problem?
Upvotes: 0
Views: 69
Reputation: 226256
Even though the list pw_in_s contains an item 'do' the if tmp in pw_in_s is not getting satisfied when tmp is 'do'.
I can't reproduce that problem.
Also the program is in an infinite loop.
That happens in the code path for if tmp in pw_in_s:
which doesn't necessarily make progress toward the termination condition on each iteration. Setting start=length
and length=1
doesn't get you closer to making start+length
bigger than len(s)
.
For debugging, change print (tmp)
to print(start, length, len(s), tmp, pw_in_s, (tmp in pw_in_s))
.
Upvotes: 2