In this video, we're going to look at how to solve Palindromes in Python
Video Transcription
Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how to use the function range to solve a very popular job interview problem called Palindrome. You could expect to get this question during a job interview, so you have to be prepared.
First, what is a Palindrome? A palindrome is a word that you can read backwards, like racecar. So, our goal here is to ask the user for a word, and then tell the user if that word is a palindrome.
My approach when I started searching for a solution was to take input. But for now, let's assume that we got racecar as the input from the user.
How are we going to solve this problem? We need to check each first letter with the last letter, next letter with the second-last letter, and so on, to see if there is a perfect match. Then that would be a palindrome.
Now, the question is: how do we split it into two halves? We could use the function range and pass the word. That would get seven characters. And if you watch my previous videos where I explained how to use floor division, we could use floor division to get the middle index. For example, if you do ‘word[3]’, you’re going to get ‘e’, because it’s in the middle.
Cool! Now, using this, we could actually pass this to the word and you see we’re getting ‘rac’ for the first half of that word. So, here I’m going to print ‘index’ and ‘word[index]’ so you see how that stuff works.
Now our goal is to compare this with the other part. So, what I’m going to do here is create a label and you see the direction. In this case, we’re moving in this direction from left to right. So, now I need to construct exactly the same thing, but this time I have to go from right to left.
So, here I’ll just print this stuff. I’ll change the direction and the reason behind these labels is to show you how that stuff works under the hood. Now what I want to do is change the index. So, what will be my index? My index will be ‘len(word) - index - 1’.
We know that ‘len(word)’ gets seven because apparently there are seven letters in this word. So, what we need to do is decrease it by ‘index’, because we need to move back. The first one on the far right would be six. So, ‘len(word) - 1’ gets six, and then we use ‘index’ to decrease that number. So, first ‘index’ would be zero, then one, and two. You watch me, I’m going to run this and now you see. First iteration, ‘index’ is zero and then here we could actually compare them.
We need to compare them and to see if that word is a palindrome. I’m going to create a flag variable, ‘is_palindrome’ and I will assign it ‘True’. So, I assume that any word you give me will be a palindrome, even if it got nothing to do with a palindrome. I assume that that’s a palindrome and then I could change that value after I run this code.
All I need to do is an ‘if’ statement. So, here I’m going to place this ‘if’ statement, and if they don’t match, then I will reassign the value of ‘is_palindrome’ to ‘False’. So, then here what I’m going to do: I’m going to grab this part from here and say ‘if not equal’. The exclamation mark (!) means ‘not equal’, and then I’ll place this part not equal to this. So, what’s going to happen is, I’ll change the value of ‘is_palindrome’ to ‘False’.
All right, and at the end of the day, all I need to do is check if it’s ‘True’ or ‘False’. So, here I don’t need this part anymore, I’m going to remove this part, and then I could say ‘if is_palindrome, then I want to print and say “word is palindrome”. Else, because there are two options, I want to say “print word is not palindrome”’.
Let’s run it on ‘racecar’. ‘Racecar’ is a palindrome. However, you could give it a try and run a bunch of different scenarios. So, you could use the ‘input()’ function and ask for a word: “Give me a word”. So, if you run this and somebody gives you ‘Apple’, then it’s not a palindrome. However, if it’s ‘racecar’, then that would be a palindrome.
Pretty simple solution here. We’re using the function range.