
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 Palindrome String Creation 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
Advertisements