Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Given a string s, representing a 12-hour clock time with am/pm, return its 24-hour equivalent.
- Example 1
- Input
- s = "05:30pm"
- Output
- "17:30"
- '''
- def solve(s):
- length = len(s)
- changeFlag = False
- if s[length-2] == 'p' and s[length-1] == 'm':
- changeFlag = True
- # changeFlag = False <----> "am"
- if changeFlag == False:
- if s[0:2] != "12":
- return s[0:5]
- else:
- return "00" + s[2:5]
- # changeFlag = True <----> "pm"
- else:
- time = int(s[0:2])
- newTime = time + 12
- if newTime != 24:
- return str(newTime) + s[2:5]
- elif newTime == 24:
- return s[0:5]
- # MAIN FUNCTION
- print(solve("05:30pm"))
- print(solve("05:30am"))
- print(solve("12:00pm"))
- print(solve("12:00am"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement