🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). Checking for palindromes is a common problem in programming interviews and competitions.
2. Problem
The problem is to determine if a given string is a palindrome. The solution needs to handle potential edge cases such as capitalization and spaces to ensure accuracy.
3. Solution Steps
1. Normalize the string by converting it to lowercase and removing non-alphanumeric characters.
2. Check if the normalized string is equal to its reverse.
3. Output the result.
4. Code Program
# Define the function to check for a palindrome
def is_palindrome(s):
# Normalize the string
normalized_string = ''.join(c for c in s.lower() if c.isalnum())
# Check if the string is equal to its reverse
return normalized_string == normalized_string[::-1]
# Given string
input_string = "A man, a plan, a canal: Panama"
# Check if the input_string is a palindrome
palindrome_status = is_palindrome(input_string)
# Print the result
print(f"Is the string a palindrome? {palindrome_status}")
Output:
Is the string a palindrome? True
Explanation:
1. The is_palindrome function takes a string s as input.
2. normalized_string is created by filtering s to include only alphanumeric characters, converting it to lowercase for case insensitivity.
3. The == operator checks if normalized_string is the same as its reverse (normalized_string[::-1]).
4. input_string is set to the phrase "A man, a plan, a canal: Panama", which is a well-known palindrome when spacing and punctuation are ignored.
5. palindrome_status receives the boolean result from the is_palindrome function.
6. The final print statement uses an f-string to present the result, indicating the string is a palindrome, denoted by True.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment