Reputation: 690
I had a internship interview today and they asked for me to talk about how I would determine whether a string is a palindrome or not using python. I set out my basic structure and some pseudocode and that all went just fine... Now that I'm home, I wanted to actually write the code.
Everything is in place, and I do a comparison of each letter in the string to determine whether or not it is a palindrome. The code works, I just have one small problem. The way I have it structured, it outputs 'The string, ~whatever~, is NOT a Palindrome.' for every single letter comparison until the last one, where it will output the final determination of whether it is a Palindrome or not.
I get why this is happening but I'm kind of stuck on how to fix it. Can someone please guide me on how to fix the if statements in my code below so it will only output whether or not it is a palindrome once?
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
Some examples of the output if I run the code are:
Enter your string here: abba
This string has an even number of letters.
The string, abba, is NOT a palindrome!
The string, abba, is a palindrome!
Enter your string here: racecar
This string has an odd number of letters.
The string, racecar, is NOT a palindrome!
The string, racecar, is NOT a palindrome!
The string, racecar, is a palindrome!
Enter your string here: travel
This string has an even number of letters.
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
The string, travel, is NOT a palindrome!
Also, if you have any recommendations on how to improve the code, please don't hesitate to send them my way. I've stayed away from other examples because I wanted this attempt to be organic, and from me.
Upvotes: 0
Views: 1160
Reputation: 114
Try the following, you might have to fix some syntax as I am not a Python dev. :)
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
bool isPalindrome =false
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] == sa[n/2 - x]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] == sa[n - a]:
isPalindrome = true
else:
//should be in a block
isPalindrome = false
break;
if isPalindrome:
print('The string, ' + string + ', is a palindrome!')
else:
print('The string, ' + string + ', is NOT a palindrome!')
Upvotes: 0
Reputation: 294
Here's the most direct way of salvaging your code:
string = raw_input('Enter your string here: ')
sa = list(string)
n = len(sa)
palindrome = True
if n%2 == 0:
print('This string has an even number of letters.')
for x in range(0, n/2):
if sa[x] != sa[n - x - 1]:
palindrome = False
else:
print('This string has an odd number of letters.')
a = int(n/2)
for x in range(0, a):
if sa[x] != sa[n - x - 1]:
palindrome = False
if palindrome:
print 'The string, ' + string + ', is a palindrome!'
else:
print 'The string, ' + string + ', is NOT a palindrome!'
First off, you don't want to report on whether it's a palindrome or not each time you check a character pair. Assume that it's a palindrome with a boolean variable and change it to False if you discover otherwise.
Next, there were a few problems with your loops and indexing. In your even case, you should be counting back from the end of the string, not the middle. Likewise, in your odd case, you should be counting back with respect to your loop variable x
, rather than the static midpoint. In both cases, you'll have to adjust your index by one to avoid errors when x = 0
.
Finally, while the approach you've taken is a good way to do it manually, there are far more concise methods you can use that are built in to the language. Simply comparing the input string against reversed(string)
or string[::-1]
will yield a boolean containing the answer to whether it's a palindrome or not.
Upvotes: 1
Reputation: 1998
Keep track of a flag during the iterations. Assign it as True
at the start, then False
later when you test for a letter that doesn't work.
Or better yet, maybe something like this would simplify things a bit:
string = 'nurses run'.replace(' ','')
gnirts = string[::-1]
if all( [ a==b for a,b in zip(string,gnirts) ] ):
print("It's a palindrome.")
else:
print("It's not a palindrome.")
Upvotes: 0