def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = input_string.strip()
print(new_string)
# Traverse through each letter of the input string
newstringseperated = new_string.split()
n = len(new_string)
if n%2 == 0:
for i in range(n//2 - 1):
if newstringseperated[i] != newstringseperated[n-1-i]:
print("False")
hello = 1
if hello == 1:
print("False")
else:
print("True")
if (n%2) != 0:
for i in range((n-1)//2):
if newstringseperated[i] != newstringseperated[n-1-i]:
hello2 = 1
if hello2 == 1:
print("False")
else:
print("True")
I tried to execute this code on the words "kayak" and "deed". It is showing index error for both of them. What is the problem here? Can someone help me find the mistake?
CodePudding user response:
First of all, to delete whitespaces, use replace()
instead of strip()
as it takes care of whitespaces in the middle of the string as well.
Secondly, the bigger problem is the split()
method. It creates a list of substrings based on a specific separator and what you are essentially doing is comparing words instead of characters. Honestly, you don't even need this method to check for palindrome, just modifying the code a bit like this should work fine:
def is_palindrome(input_string):
new_string = input_string.replace(" ", "")
n = len(new_string)
for i in range(n // 2):
if new_string[i] != new_string[n - 1 - i]:
print("False")
return False
print("True")
return True
CodePudding user response:
You have a number of problems here. As @John says, you want to use n // 2 - 1
rather than n / 2 - 1
so that the result is an integer. If you use re.sub()
instead of split()
, you can get rid of whitespace in the middle of your input strings and get rid of tabs as well as spaces. The big issue is that splitting the input string to create newstringseperated
and using that is messing you up. If you instead operate on new_string
directly, your code will work. Another small detail...you can break
as soon as you recognize a mismatch. This version of your code does what I think you're expecting:
import re
def is_palindrome(input_string):
# We'll create two strings, to compare them
new_string = re.sub(r'\s ', '', input_string)
print(new_string)
# Traverse through each letter of the input string
# newstringseperated = new_string.split()
n = len(new_string)
if n % 2 == 0:
hello = 0
for i in range(n // 2 - 1):
if new_string[i] != new_string[n - 1 - i]:
hello = 1
break
if hello == 1:
print("False")
else:
print("True")
# Add any non-blank letters to the
# end of one string, and to the front
# of the other string.
if (n % 2) != 0:
hello2 = 0
for i in range((n - 1) // 2):
if new_string[i] != new_string[n - 1 - i]:
hello2 = 1
break
if hello2 == 1:
print("False")
else:
print("True")
is_palindrome("kayak")
is_palindrome("deed")
is_palindrome("abcde")
is_palindrome("abcd")
Result:
kayak
True
deed
True
abcde
False
abcd
False
It is better to not have the two cases (odd vs even lengths) in your code. Here's a way to have just one version of your inner logic:
import re
def is_palindrome(input_string):
new_string = re.sub(r'\s ', '', input_string)
print(new_string)
# Traverse through each letter of the input string
n = len(new_string)
for i in range(n // 2 - 1 n % 2):
if new_string[i] != new_string[n - 1 - i]:
hello = 1
break
else:
hello = 0
print("False" if hello == 1 else "True")
This produces the same result.
CodePudding user response:
I had to do this on my phone but this should work if your looking for a palindrome:
txt = "hannah"
txt2 = "kayak"
txt3 = "blaat"
def palin(txt):
first_half = len(txt) // 2
start_second_half = first_half -1 if len(txt)%2==0 else first_half
return txt[:first_half] == txt[-1:start_second_half:-1]
print(palin(txt))
print(palin(txt2))
print(palin(txt3))
CodePudding user response:
Just reverse the string and test so no looping needed:
def is_palindrome(txt):
txt = txt.replace(' ', '')
return txt == txt[::-1]