125. Valid Palindrome in Python3

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!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why you should take a modern approach in dockerizing selenium tests

Azure Application Insights

Managing AI-Powered Products — Important Principles

Ultimate Guide on How to hire Flutter developer

Ultimate Guide on How to hire Flutter developer

The Escape Key is a Feature

Recommended Native Development Online Courses 2020

Docker With Spring Boot Setup

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Melissa Guachun

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.

More from Medium

Recursion Simplified

The Coding Survival Guide.

What is Recursion in programming?