Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # program to find the next smallest palindrome
- def findNextSmallestPalindrome(inputString):
- # calculate length of the string
- length = len(inputString)
- comString = '9'*length
- #if the number is less than 11 then the smallest palindrome is 11
- if int(inputString) < 11 :
- print("The next smallest palindrome is : 11")
- return
- #checking if the string has all '9's (case 1)
- if inputString == comString:
- finalString = '0'*(length-1)
- finalString = '1' + finalString + '1'
- print("The next smallest palindrome is : " + finalString)
- del comString
- return
- # case2 where the input not all digits as 9
- else:
- # to make manipulation easy
- # convert string into list
- myStr = list(map(int,inputString))
- # calculate mid positions
- if int(length%2) == 0:
- i = int(length/2) - 1
- j = i+1
- else:
- i = int(length/2) - 1
- j = i+2
- # parsed indicates if the operation is done on the input string
- # set parsed to false
- parsed = False
- while not parsed:
- #checking if the entered number is already a palindrome or not
- alreadyPalindrome = True
- #spliting the list from the middle and then reversing the second before comparison
- temp1 = myStr[:i+1]
- temp2 = myStr[j:]
- temp2.reverse()
- if temp1 != temp2:
- alreadyPalindrome = False
- if alreadyPalindrome:
- # if the entered string is already a palindrome
- # then compute mid position and increment the digits by 1
- if int(length%2) == 0:
- i = int(length/2) - 1
- j = i+1
- # if the middle digits are not 9 then add 1 to them
- if myStr[i] != 9 and myStr[j] != 9:
- myStr[i] += 1
- myStr[j] += 1
- myStr = list(map(str, myStr))
- finalString2 = "".join(myStr)
- else:
- # while the middle digits are 9 replace them by 0
- while myStr[i] == 9:
- myStr[i] = 0
- myStr[j] = 0
- i -= 1
- j += 1
- # add 1 to the digits that are not 9
- myStr[i] += 1
- myStr[j] += 1
- myStr = list(map(str, myStr))
- finalString2 = "".join(myStr)
- print("The next smallest palindrome is : " + finalString2)
- parsed = True
- else:
- i = int(length/2)
- # if the middle digit is not 9 then add 1 to it
- if myStr[i] != 9:
- myStr[i] += 1
- else:
- if myStr[i] == 9:
- myStr[i] = 0
- j = i+1
- i -= 1
- # while the middle digits are 9 replace them by 0
- while myStr[i] == 9:
- myStr[i] = 0
- myStr[j] = 0
- i -= 1
- j += 1
- myStr[i] += 1
- myStr[j] += 1
- myStr = list(map(str,myStr))
- finalString2 = ''.join(myStr)
- print("The next smallest palindrome is : " + finalString2)
- parsed = True
- #this block is if the entered number is not already a palindrome
- else:
- # find positions of non matching digits
- while myStr[i] == myStr[j]:
- i -= 1
- j += 1
- # if the number on the left is greater than number in the right
- # copy the left side to right side
- if int(myStr[i]) > int(myStr[j]):
- while i != 0:
- myStr[j] = myStr[i]
- i -= 1
- j += 1
- myStr[j] = myStr[i]
- myStr = list(map(str,myStr))
- finalString = ''.join(myStr)
- print("The next smallest palindrome is : " + finalString)
- parsed = True
- # if the number on the left is less than number in the right
- # increment all the elements between i to j
- # copy elements to left of i to elements to right of j including i,j
- else:
- temppos = i
- # increment all the elements between i to j
- for k in range(i+1,j):
- myStr[k] += 1
- # if there are no elements between i and j
- # then copy jth element to ith element
- if i+1 == j:
- myStr[i] = myStr[j]
- i -= 1
- j += 1
- # copy left side to right side
- while i != 0:
- myStr[j] = myStr[i]
- i -= 1
- j += 1
- myStr[j] = myStr[i]
- #print(myStr)
- myStr = list(map(str,myStr))
- finalString = ''.join(myStr)
- print("The next smallest palindrome is : " + finalString)
- parsed = True
- # main function
- next = True
- while next:
- inputString = input("Enter the number : ")
- findNextSmallestPalindrome(inputString)
- n = int(input("Do You Want to try again (1/0) : "))
- if n == 1:
- next = True
- elif n == 0:
- next = False
- print()
Add Comment
Please, Sign In to add comment