Problem p03150 (python)

Fixed submission:
A=["k","e","y","e","n","c","e"]
f=-1;p=0;q=0;r=0
S=list(map(str,input()))
if S[0:7]==A or S[len(S)-7:len(S)]==A:
  f=1
else:
  if S[0]!=A[0]:
    f=0
  else:    
    for i in range(len(S)):
      if S[i]==A[i]:
        p=p+1
      else:
        if A[0:p]+S[len(S)-(len(A)-p):len(S)]==A:
          #print(A[0:p]+S[len(S)-(len(A)-p):len(S)])
          f=1
          break
        else:
          f=0
          break
print("YES" if f==1 else "NO")

Buggy submission (verdict: Runtime Error):
A=["k","e","y","e","n","c","e"]
f=-1;p=0;q=0;r=0
S=list(map(str,input()))
if S[0:7]==A or S[len(S)-7:len(S)]==A:
  f=1
else:
  if S[0]!=A[0]:
    f=0
  else:    
    for i in range(len(S)):
      if S[i]==A[i-q]:
        p=p+1
        r=0
      else:
        if q>0 and r==0:
          f=0
          q=q+1
          
        else:
          r=1
          q=q+1
if f==0:
  print("NO")
  
elif f==1:
  print("YES")
  
else:
  if p==7 and (p+q)==len(S):
    print("YES")
    
  else:
    print("NO")

Buggy submission (verdict: Runtime Error):
A=["k","e","y","e","n","c","e"]
f=-1;p=0;q=0;r=0
S=list(map(str,input()))
if S[0:7]==A or S[len(S)-7:len(S)]==A:
  f=1
else:
  if S[0]!=A[0]:
    f=0
  else:    
    for i in range(len(S)):
      if S[i]==A[i-q]:
        p=p+1
        r=0
      else:
        if q>0 and r==0:
          f=0
          break
        else:
          r=1
          q=q+1
if f==0:
  print("NO")
  
elif f==1:
  print("YES")
  
else:
  if p==7 and (p+q)==len(S):
    print("YES")
    
  else:
    print("NO")

Buggy submission (verdict: Runtime Error):
def my_index(l, x, default=0):
    if x in l:
        return l.index(x)
    else:
        return default


T=list("keyence")
x=0
j=0
S=input()
#print(my_index(S,"k"))
if my_index(S,"k")==0:
  for i in range(len(S)):
    if S[i]==T[j]:
      x=x+1
      #print(x)
      j=j+1
    else:
      #print("c")
      continue
  if x==7:
    print("YES")
  else:
    print("NO")    
else:
  for i in range(int(my_index(S,"k")),len(S)):
    if S[i]==T[j] and int(my_index(S,"k"))+7==len(S):
      x=x+1
      #print(x,i)
      j=j+1
    else:
      break
  if x==7 and int(my_index(S,"k"))+7==len(S):
    print("YES")
  else:
    print("NO")

Buggy submission (verdict: Runtime Error):
def my_index(l, x, default=0):
    if x in l:
        return l.index(x)
    else:
        return default


T=list("keyence")
x=0
j=0
S=input()
#print(my_index(S,"k"))
if my_index(S,"k")==0:
  for i in range(len(S)):
    if S[i]==T[j]:
      x=x+1
      #print(x)
      j=j+1
    else:
      #print("c")
      continue
  if x==7:
    print("YES")
  else:
    print("NO")    
else:
  for i in range(int(my_index(S,"k")),len(S)):
    if S[i]==T[j]:
      x=x+1
      #print(x,i)
      j=j+1
    else:
      print("NO")
      break
  if x==7:
    print("YES")

Buggy submission (verdict: Runtime Error):
def my_index(l, x, default=0):
    if x in l:
        return l.index(x)
    else:
        return default


T=list("keyence")
x=0
j=0
S=input()
if my_index(S,"k")==0:
  for i in range(len(S)):
    if S[i]==T[j]:
      x=x+1
      j=j+1
    else:
      continue
  if x==7:
    print("YES")
  else:
    print("NO")
else:
  for i in range(int(my_index(S,"k")),int(my_index(S,"k"))+7):
    if S[i]==T[i]:
      x=x+1
    if x==7:
      print("YES")
    else:
      print("NO")