Created
January 18, 2020 22:36
-
-
Save mcxemic/232ad49b1dc858ae6949ef67f050bfd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
MAX_INT = sys.maxsize | |
def get_time_from_string(time_string): | |
time_value = "".join(re.split("[a-zA-Z]", time_string)) | |
time_format = "".join(re.split('\d+:\d+', time_string)).lower() | |
[hours, minutes] = time_value.split(":") | |
hours = int(hours) if int(hours) < 12 else 0 | |
minutes = int(minutes) | |
time_in_minutes = hours * 60 + minutes | |
if time_format == 'pm' and hours < 12: | |
time_in_minutes += 720 # 12hours in min | |
return time_in_minutes | |
def get_min_diff(arr): | |
times_in_minutes = [get_time_from_string(i) for i in arr] | |
times_in_minutes.sort(reverse=True) | |
min_diff = MAX_INT | |
i = 0 | |
while i<len(times_in_minutes)-1: | |
diff = times_in_minutes[i] - times_in_minutes[i+1] | |
if diff < min_diff: | |
min_diff = diff | |
i += 1 | |
print(min_diff) | |
return min_diff | |
get_min_diff(["10:00am", "11:45pm", "5:00am", "12:01am"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment