Tattarattat (knocks on door). It's Day 3 and today is all about Palindromes!!! Palin-the-what-now?
A Palindrome is a word, number, phrase or sentence that reads the same backwards or forwards. Examples include mom, racecar, tattarattat, 1234321, "was it a car or a cat i saw", etc.
Instructions For Today's Task
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome when it reads the same backward as forward.
For example, 121 is a palindrome while 123 is not.
TestCase Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
My Solution
Thinking about palindromes, the first thing that came to mind was to compare the integer given(forward) with its reverse(backward).
To do this, i thought of slicing the integer by ommitting the start and stop values(this indexes the first and last values) and using a step value of minus one to collect the number from backwards. However, this only works for strings, lists and tuples and not integers.
To solve this issue, i decided to convert the integer to string before comparing with the reverse.
My first attempt had an if statement to check if the number was positive before comparing with its reverse and return true if it was a palindrome. It was also to return to return false if the number was negative as negative integers can never be palindromes due to the minus sign.
However, my first attempt didn't include an else statement to return false if the integer was not a palindrome. I made the correction and tried the second time.
My second attempt had another flaw. It didn't account for cases where the integer was equal to zero. I made the correction and voila! All testcases passed!
You can view my submission details below
Onwards to Day 4!