125. Valid Palindrome in Python3

Melissa Guachun
3 min readJan 18, 2022

--

Photo by Raphael Schaller on Unsplash

What I find amazing is that with any language, there are always a plethora of built in methods waiting to be discovered and used. This was very much the case for this LeetCode Easy problem.
The challenge wants us to create an algorithm that returns a boolean determining if the input if a palindrome or not. Seems doable right? Let’s review the problem for any parameters to keep in mind:

  • To make sure we’re all on the same page, a palindrome is a word that reads the same word forward and backwards
  • For this case, all of the uppercase letters must be made lowercase
  • The problem uses the word alphanumeric which had me a bit stumped, but it basically means characters from A-Z, a-z, and 0–9 are what we are going to be focusing on
  • We will be removing all non alphanumeric numbers, so we must take note that this includes empty spaces between words

On paper I try to write down the steps needed to get to our solution:

//determine if our all letters of our input are alphanumeric
// if alphanumeric then we change them to lower case
// keep our output in a place for comparison

But then I remember that a palindrome must be the same forwards and backwards. So at some point, we have to iterate backwards and compare our output from going forwards. So I added to my notes:

-reverse the test
-if the forward test and backwards test match, return true
-else return false

Given our starter code:

def isPalindrome(self, s:str)
test=[]
for i in s:

The next step we can take is to use a built in method that was taught to me by a friend. It’s called isalnum(). It's a great built in method that determines if the argument if an alphanumeric number.
With this nugget of knowledge, we can use it to help us return a boolean of True or False. If the input is alphanumeric, we return True.

def isPalindrome(self, s:str)
test=[]
for i in s:
if i.isalnum() == True:

But what do we do once we know it’s alphanumeric? We have to keep it/add it somewhere. This is our hint to use the built in method append(). We use our space test to store our alphanumeric inputs. While we store them, we can employ another handy built in method, .lower(). Remember to add the empty () in .lower() to invoke it!

def isPalindrome(self, s:str)
test=[]
for i in s:
if i.isalnum() == True:
test.append(i.lower())

Now that we’ve store the alphanumeric characters and made them lowercase, we must do a reverse test where we will iterate from right to left. What I learned is to iterate backwards, we can reuse our test=[] by revering it's direction with [::-1] in python3. We can make the reversed test identifiable by a variable called 'revtest'.

def isPalindrome(self, s:str)
test=[]
for i in s:
if i.isalnum() == True:
test.append(i.lower())
revtest = test[::-1]

Once the reverse test has been done, we can compare the two tests and determine if the input is True or False.

def isPalindrome(self, s:str)
test=[]
for i in s:
if i.isalnum() == True:
test.append(i.lower())
revtest = test[::-1]
if(test == revtest):
return True
else:
return False

Conclusion:
This solution is just one of many and being a newer Pythonista it’s important to understand that there are always going to be more than one way to solve a problem. This solution uses space because we’re using space to store test=[] and for revtest = test[::-1]. While this solution makes sense to me, it' advantageous to uncover other solutions and approaches. Since this is an algorithm problem, an assessor in a technical assessment might not want you to use built in methods or short cuts. But for the time being, I'm still in the very early stages of solving algorithms. So I try not to let this overwhelm me. But I hope this helps!

--

--

Melissa Guachun
Melissa Guachun

Written by Melissa Guachun

Software Developer and visual artist based in NYC. Join me on my journey to coding enlightenment or a torrential mental breakdown, whichever comes first.

No responses yet