

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Check if it is possible to create a polygon with given n sidess in Python
- Check if it is possible to create a polygon with a given angle 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
- 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
- Check if it is possible to move from (0, 0) to (x, y) in N steps 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 is possible to get given sum from a given set of elements in Python
- Python program to check if a string is palindrome or not
- Is it possible to check if a String only contains ASCII in java?
- Check if it is possible to survive on Island in Python
Advertisements