Palindrome-Program-and-Algorithm-in-Python-2023-mymixindia.com

Palindrome Program and Algorithm in Python 2023


 

Palindrome Program and Algorithm in Python 2023 #palindrome #algorithm #python

A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward.

example (numbers) : 1001, 23032, 112232211, 123454321 etc.

example (string) : WOW, NUN, SIS, POP, LEVEL, MADAM, STATS, etc.

In Python, we can write a program to check if a given string is a palindrome or not.

Here’s an example:

def is_palindrome(string):
"""
It will return true if the given is a palindrome, otherwise it will return false
"""
#remove any whitespace and convert to lowercase
string  = sting.replace(" ","").lower()
#check is the string is equal to its reverse
return string == string[::-1]

#Now Test the function
print(is_palindrome("racecare")) #True
print(is_palindrome("hello")) #False



 

Output:

True
False

Here, we define a function `is_palindrome` that takes a string as input and returns `True` if it is a palindrome, and `False` if it is not.

To check if a string is a palindrome, we first remove any whitespace and convert it to lowercase. We then compare the string to its reverse using slicing notation `string[::-1]`. This creates a new string that is the reverse of the original string, and we check if it is equal to the original string.

In the example above, we test the `is_palindrome` function with two sample strings: “racecar” and “hello”. The first string is a palindrome, so the function returns `True`, while the second string is not a palindrome, so the function returns `False`.

Note that this program only works for strings that contain letters and whitespace. If you want to check palindromes of numbers or other characters, you may need to modify the program accordingly.

 


 

Palindrome-Program-Algorithm-and-Time-Complexity-in-Python-2023-mymixindia.com

Palindrome Program Algorithm

Here is a step-by-step algorithm for checking whether a given string is a palindrome:

1. Define a function to check if a given string is a palindrome.
2. Take the input string as a parameter to the function.
3. Remove any whitespace or punctuation from the input string.
4. Convert the input string to lowercase to ensure that case does not affect the result.
5. Initialize two pointers, one at the beginning of the string (i.e., index 0) and the other at the end of the string (i.e., index n-1, where n is the length of the string).
6. While the two pointers have not crossed over each other, compare the characters at the current positions of the pointers.
7. If the characters are not equal, the string is not a palindrome, so return False.
8. If the characters are equal, move the pointers towards each other, one step at a time (i.e., increment the left pointer and decrement the right pointer).
9. If the pointers cross over each other, the string is a palindrome, so return True.

Here is the same algorithm expressed as Python code:

def is_palindrome(string):
# Remove whitespace and punctuation from the string
string = ''.join(e for e in string if e.isalnum()).lower()

# Initialize pointers
left = 0
right = len(string) - 1

# Compare characters at the current positions of the pointers
while left < right:
if string[left] != string[right]:
return False
left += 1
right -= 1

# If the pointers have crossed over each other, the string is a palindrome
return True

 

This algorithm is very efficient and has a time complexity of O(n), where n is the length of the input string.


 

Time Complexity of Palindrome Program

The time complexity of a palindrome program depends on the approach used to check if the given string is a palindrome or not.

The most common approach is to use two pointers, one starting from the beginning of the string and the other starting from the end of the string, and then compare the characters at each position until the two pointers meet at the middle of the string. This approach has a time complexity of O(n), where n is the length of the input string. This is because we are iterating through the string only once, comparing each character at most once.

 


Also you can download the python from here : https://www.python.org/downloads/

Read this : How to make a compiler in c/cpp