'Expecting One line as output for the python 3 script but the result is varying
I am new to Python
I am trying to solve this problem. https://www.hackerrank.com/challenges/palindrome-index/problem
my code is here.
def palindrome(s):
string = ''
if s == s[::-1]:
print(-1) # return -1
else:
for i in range(len(s)):
string = s[:i]+s[i+1:]
if string == string[::-1]:
print(i) # return i
break
else:
if(i+1 == len(s)):
print(-1) # return -1
when I submitted the above code, I got some test cases passed and 3 test cases are "time limit exceeded".
Then I have gone through this video https://www.youtube.com/watch?v=gd4hCrbYAMg and tried the code given in the video as below.
def palindromeIndex(s):
if s == s[::-1]:
print(-1) # return -1
n = len(s)
for i in range(n//2):
if s[i] != s[n-1-i]:
if s[i:n-1-i] == s[i:n-1-i][::-1]:
print(n-1-i) # return n-1-i
elif s[i+1:n-i] == s[i+1:n-i][::-1]:
print(i) # return i
print(-1) # return -1
from the video, code above is successfully passed all test cases.
when I try to run above code in my terminal with plain text file with .py extension, it is giving 3 lines or 2 lines or 1line as a result for youtube linked script.
My Question is about the script from video link(which is palindromeIndex(s)), Can anyone explain why is this happening please?
example:
# palindrom.py
#!/bin/python3
#####################################################################
def palindromeIndex(s):
if s == s[::-1]:
print(-1)
n = len(s)
for i in range(n//2):
if s[i] != s[n-1-i]:
if s[i:n-1-i] == s[i:n-1-i][::-1]:
print(n-1-i)
elif s[i+1:n-i] == s[i+1:n-i][::-1]:
print(i)
print(-1)
#####################################################################
#####################################################################
def palindrome(s):
string = ''
if s == s[::-1]:
print(-1)
else:
for i in range(len(s)):
string = s[:i]+s[i+1:]
if string == string[::-1]:
print(i)
break
else:
if(i+1 == len(s)):
print(-1)
#####################################################################
s = input()
print('########### result of the script from video link ##############')
palindromeIndex(s)
print('################ result of the script by me ###################')
palindrome(s)
print('###########################################')
$ python palindrome.py
yellow
########### result of the script from video link ##############
-1
################ result of the script by me ###################
-1
###########################################
$ python palindrome.py
red
########### result of the script from video link ##############
-1
################ result of the script by me ###################
-1
###########################################
$ python palindrome.py
azad
########### result of the script from video link ##############
3
2 # } Not Expecting
-1 # } Not Expecting
################ result of the script by me ###################
3
###########################################
$ python palindrome.py
pratap
########### result of the script from video link ##############
1
3 # } Not Expecting
-1 # } Not Expecting
################ result of the script by me ###################
1
###########################################
$
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

