Posts

Showing posts from April, 2023

Palindromic Questions

Image
Print all the palindromic numbers from 1 to 500 in Python: for num in range ( 1 , 501 ): if str (num) == str (num)[::- 1 ]: print (num) Write a Python function to check if a given string is a palindrome or not. def   is_palindrome ( string ):      return  string == string[::- 1 ] Write a Python function to check if a given integer is a palindrome or not. def is_palindrome ( number ): return str (number) == str (number)[::- 1 ] Write a Python function to count the number of palindromic substrings in a given string. def count_palindromic_substrings ( string ): count = 0 for i in range ( len (string)): for j in range (i+ 1 , len (string)+ 1 ): if string[i:j] == string[i:j][::- 1 ]: count += 1 return count Write a Python function to find the longest palindromic substring in a given string. def longest_palindromic_substring ( string ): max_len = 0 longest_substr = '' for i in ...