m2skills

smallest palindrome python

Apr 18th, 2017
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.41 KB | None | 0 0
  1. # program to find the next smallest palindrome
  2.  
  3. def findNextSmallestPalindrome(inputString):
  4.    
  5.     # calculate length of the string
  6.     length = len(inputString)
  7.     comString = '9'*length
  8.    
  9.     #if the number is less than 11 then the smallest palindrome is 11
  10.     if int(inputString) < 11 :
  11.         print("The next smallest palindrome is : 11")
  12.         return
  13.        
  14.     #checking if the string has all '9's (case 1)
  15.     if inputString == comString:
  16.         finalString = '0'*(length-1)
  17.         finalString = '1' + finalString + '1'
  18.         print("The next smallest palindrome is : " +  finalString)
  19.         del comString
  20.         return
  21.    
  22.     # case2 where the input not all digits as 9
  23.     else:
  24.         # to make manipulation easy
  25.         # convert string into list
  26.         myStr = list(map(int,inputString))
  27.        
  28.         # calculate mid positions
  29.         if int(length%2) == 0:
  30.             i = int(length/2) - 1
  31.             j = i+1
  32.         else:
  33.             i = int(length/2) - 1
  34.             j = i+2
  35.  
  36.         # parsed indicates if the operation is done on the input string
  37.         # set parsed to false  
  38.         parsed = False
  39.         while not parsed:
  40.             #checking if the entered number is already a palindrome or not
  41.             alreadyPalindrome = True
  42.             #spliting the list from the middle and then reversing the second before comparison
  43.             temp1 = myStr[:i+1]
  44.             temp2 = myStr[j:]
  45.             temp2.reverse()
  46.            
  47.             if temp1 != temp2:
  48.                 alreadyPalindrome = False
  49.    
  50.    
  51.             if alreadyPalindrome:
  52.                 # if the entered string is already a palindrome
  53.                 # then compute mid position and increment the digits by 1
  54.                 if int(length%2) == 0:
  55.                     i = int(length/2) - 1
  56.                     j = i+1
  57.                    
  58.                     # if the middle digits are not 9 then add 1 to them
  59.                     if myStr[i] != 9 and myStr[j] != 9:
  60.                         myStr[i] += 1
  61.                         myStr[j] += 1
  62.                         myStr = list(map(str, myStr))
  63.                         finalString2 = "".join(myStr)
  64.                     else:
  65.                         # while the middle digits are 9 replace them by 0
  66.                         while myStr[i] == 9:
  67.                             myStr[i] = 0
  68.                             myStr[j] = 0
  69.                             i -= 1
  70.                             j += 1
  71.  
  72.                         # add 1 to the digits that are not 9   
  73.                         myStr[i] += 1
  74.                         myStr[j] += 1
  75.                        
  76.                         myStr = list(map(str, myStr))
  77.                         finalString2 = "".join(myStr)
  78.                        
  79.                     print("The next smallest palindrome is : " + finalString2)
  80.                     parsed = True
  81.                
  82.                 else:
  83.                     i = int(length/2)
  84.                    
  85.                     # if the middle digit is not 9 then add 1 to it
  86.                     if myStr[i] != 9:
  87.                         myStr[i] += 1
  88.  
  89.                     else:
  90.                         if myStr[i] == 9:
  91.                             myStr[i] = 0
  92.                         j = i+1
  93.                         i -= 1
  94.                        
  95.                         # while the middle digits are 9 replace them by 0
  96.                         while myStr[i] == 9:
  97.                             myStr[i] = 0
  98.                             myStr[j] = 0
  99.                             i -= 1
  100.                             j += 1     
  101.                        
  102.                         myStr[i] += 1
  103.                         myStr[j] += 1
  104.                        
  105.                     myStr = list(map(str,myStr))
  106.                     finalString2 = ''.join(myStr)
  107.                     print("The next smallest palindrome is : " + finalString2)
  108.                     parsed = True
  109.                    
  110.             #this block is if the entered number is not already a palindrome
  111.             else:
  112.                 # find positions of non matching digits
  113.                 while myStr[i] == myStr[j]:
  114.                     i -= 1
  115.                     j += 1
  116.  
  117.                 # if the number on the left is greater than number in the right
  118.                 # copy the left side to right side
  119.                 if int(myStr[i]) > int(myStr[j]):
  120.                     while i != 0:
  121.                         myStr[j] = myStr[i]
  122.                         i -= 1
  123.                         j += 1
  124.                     myStr[j] = myStr[i]
  125.                     myStr = list(map(str,myStr))
  126.                     finalString = ''.join(myStr)
  127.                     print("The next smallest palindrome is : " + finalString)
  128.                     parsed = True
  129.                
  130.                 # if the number on the left is less than number in the right
  131.                 # increment all the elements between i to j
  132.                 # copy elements to left of i to elements to right of j including i,j
  133.                 else:
  134.                     temppos = i
  135.                     # increment all the elements between i to j
  136.                     for k in range(i+1,j):
  137.                         myStr[k] += 1
  138.                    
  139.                     # if there are no elements between i and j
  140.                     # then copy jth element to ith element
  141.                     if i+1 == j:
  142.                         myStr[i] = myStr[j]
  143.                         i -= 1
  144.                         j += 1
  145.                    
  146.                     # copy left side to right side
  147.                     while i != 0:
  148.                         myStr[j] = myStr[i]
  149.                         i -= 1
  150.                         j += 1
  151.                     myStr[j] = myStr[i]
  152.                     #print(myStr)
  153.                     myStr = list(map(str,myStr))
  154.                     finalString = ''.join(myStr)
  155.                     print("The next smallest palindrome is : " +  finalString)
  156.                     parsed = True
  157.                    
  158.                    
  159. # main function
  160. next = True
  161. while next:
  162.     inputString = input("Enter the number : ")
  163.     findNextSmallestPalindrome(inputString)
  164.     n = int(input("Do You Want to try again (1/0) : "))
  165.     if n == 1:
  166.         next = True
  167.     elif n == 0:
  168.         next = False
  169.     print()
Add Comment
Please, Sign In to add comment