Advertisement
makispaiktis

BinarySearchIO - 24-hour time

Aug 23rd, 2020 (edited)
1,881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. '''
  2. Given a string s, representing a 12-hour clock time with am/pm, return its 24-hour equivalent.
  3. Example 1
  4. Input
  5. s = "05:30pm"
  6. Output
  7. "17:30"
  8. '''
  9.  
  10. def solve(s):
  11.     length = len(s)
  12.     changeFlag = False
  13.     if s[length-2] == 'p' and s[length-1] == 'm':
  14.         changeFlag = True
  15.     # changeFlag = False <----> "am"
  16.     if changeFlag == False:
  17.         if s[0:2] != "12":
  18.             return s[0:5]
  19.         else:
  20.             return "00" + s[2:5]
  21.     # changeFlag = True <----> "pm"
  22.     else:
  23.         time = int(s[0:2])
  24.         newTime = time + 12
  25.         if newTime != 24:
  26.             return str(newTime) + s[2:5]
  27.         elif newTime == 24:
  28.             return s[0:5]
  29.  
  30. # MAIN FUNCTION
  31. print(solve("05:30pm"))
  32. print(solve("05:30am"))
  33. print(solve("12:00pm"))
  34. print(solve("12:00am"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement