forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext_Palindrome.py
More file actions
43 lines (31 loc) · 781 Bytes
/
Next_Palindrome.py
File metadata and controls
43 lines (31 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'''
Python Program to get the next nth palindrome of a given number.
An integer is a palindrome if the reverse of that number is
equal to the original number.
'''
def isPalindrome(num):
return str(num) == str(num)[::-1]
def getNthPalindrome(n, start):
if n < 1:
return start
while not isPalindrome(start + 1):
start += 1
return getNthPalindrome(n - 1, start + 1)
if __name__ == '__main__':
num = int(input('Enter a Number: '))
n = int(input('Enter nth position: '))
print(getNthPalindrome(n, num))
'''
Sample Case:
Example 1:
Enter a number: 12021
Enter nth position: 10
13031
Example 2:
Enter a number: 141
Enter nth position: 5
191
Time Complexity: O(10^k)
Space Complexity: O(1)
where k = no of digits in given number
'''