
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the time which is palindromic and comes after the given time in Python
Suppose we have a string s that represents a time in the 24 hours format as HH:MM so that HH will be in range 0 to 23 and MM will be in range 0 to 59, We have to find the next closest time which is a palindrome when read as a string. If there is no such string, then return -1.
So, if the input is like "22:22", then the output will be "23:32".
To solve this, we will follow these steps −
n := size of s
hour_string := substring of s[from index 0 to 2]
minute := substring of s[from index 3 to 5] and convert it to integer
rev_hour := reverse the hour_string and convert it to number
rev_hr_str := reversed of hour_string
h := hour_string as integer
temp := blank string, res := blank string
if h is 23 and minute >= 32, then
res := -1
otherwise when minute < rev_hour , then
if h < 10, then
temp := "0"
temp := temp concatenate h
if rev_hour < 10, then
res := res concatenate temp concatenate ":0" concatenate rev_hr_str
otherwise,
res := res concatenate temp concatenate ":" concatenate rev_hr_str
otherwise,
h := h + 1
rev_hr_str := reversed of h as string
rev_hour := reversed of h
if h < 10, then
temp := "0"
temp := temp concatenate h
if rev_hour < 10, then
res := res concatenate temp concatenate ":0" concatenate rev_hr_str
otherwise,
res := res concatenate temp concatenate ":" concatenate rev_hr_str
return res
Example
Let us see the following implementation to get better understanding −
def get_next_palindrome_time(s) : n = len(s) hour_string = s[0 : 2] minute = int(s[3 : 5]) rev_hour = int(hour_string[::-1]) rev_hr_str = hour_string[::-1] h = int(hour_string) temp = "" res = "" if (h == 23 and minute >= 32) : res = "-1" elif (minute < rev_hour) : if (h < 10) : temp = "0" temp = temp + str(h) if (rev_hour < 10) : res = res + temp + ":0" + rev_hr_str else : res = res + temp + ":" + rev_hr_str else : h += 1 rev_hr_str = str(h)[::-1] rev_hour = int(rev_hr_str) if (h < 10) : temp = "0" temp = temp + str(h) if (rev_hour < 10) : res = res + temp + ":0" + rev_hr_str else : res = res + temp + ":" + rev_hr_str return res s = "22:22" print(get_next_palindrome_time(s))
Input
"22:22"
Output
23:32
- Related Articles
- Find the minimum time after which one can exchange notes in Python
- Python program to find difference between current time and given time
- Python Pandas - Find the end time for the given Period object
- Python Pandas - Find the start time for the given Period object
- Program to find nearest time by reusing same digits of given time in python
- Find a time for which angle between hour and minute hands is given theta in C++
- Program to find expected growth of virus after time t in Python
- How to get Time in Milliseconds for the Given date and time in Java?
- Which physical quantity is given by the slope of distance-time graph?
- How to access and convert time using the time library in Python
- Find the arrangement of queue at given time in C++
- The time Module in Python
- Find minimum time to finish all jobs with given constraints in Python
- Program to find length of longest palindromic substring after single rotation in Python
- Find the index which is the last to be reduced to zero after performing a given operation in Python
