
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Check if it is possible to create a palindrome string from given N in Python
Suppose we have a number n. We have to check whether we can create an alphabetical lowercase string from that number and check whether the string is palindrome or not. Here we will take only characters from a to j, [a = 0, b = 1... j = 9]. So if the number is 42 the substring "ec" will be printed till 6 (4+2) characters "ececec" then check this is palindrome or not.
So, if the input is like n = 43, then the output will be True the string is "ededede" and this is palindrome.
To solve this, we will follow these steps −
- temp := blank string
- s := n as string
- letters := all characters from a to j
- sum := 0
- substr := blank string
- for i in range 0 to size of s - 1, do
- d := s[i] as numeric digit
- substr := substr concatenate letters[d]
- sum := sum + d
- while size of temp <= sum, do
- temp := temp concatenate substr
- temp := temp[from index 0 to sum - 1]
- return true when temp is palindrome, otherwise false
Example
Let us see the following implementation to get better understanding −
def isPalindrome(s): return s == s[::-1] def solve(n): temp = "" s = str(n) letters = "abcdefghij" sum = 0 substr = "" for i in range(len(s)) : d = int(s[i]) substr += letters[d] sum += d while len(temp) <= sum: temp += substr temp = temp[:sum] return isPalindrome(temp) n = 43 print (solve(n))
Input
43
Output
True
- Related Articles
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to form string B from A under the given constraint in Python
- Python program to check if a given string is number Palindrome
- Check if it is possible to create a polygon with a given angle in Python
- Python program to check if the given string is vowel Palindrome
- C Program to Check if a Given String is a Palindrome?
- Check if it is possible to convert one string into another with given constraints in Python
- Check if it is possible to transform one string to another in Python
- Python program to check if a string is palindrome or not
- Check if it is possible to move from (0, 0) to (x, y) in N steps in Python
- Check if is possible to get given sum from a given set of elements in Python
- TCP Client-Server Program to Check if a Given String is a Palindrome
- Check if a given string is a rotation of a palindrome in C++
- Check if characters of a given string can be rearranged to form a palindrome in Python
- Is it possible to check if a String only contains ASCII in java?

Advertisements