Last active
May 28, 2021 04:31
-
-
Save IEdiong/5ee7a1a2b531ba08c9e9e89833c2649e to your computer and use it in GitHub Desktop.
This program was created to help Esther (our fictitious client) to find the minimum time difference between any two of her wake up times recorded in her journal
This file contains hidden or 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
from itertools import combinations | |
def minDifference(wakeTimes): | |
# if wakeTimes is an empty array | |
if not wakeTimes: | |
return 0 | |
# creates an array of wakeTimes in minute | |
wakeTimesInMins = list(map( lambda x: int(x[0]) * 60 + int(x[1]), [ time.split(":") for time in wakeTimes ])) | |
# ignores all invalid input i.e. any time entry that exceeds 23:59 === 1439 | |
wakeTimesInMins = list(filter( lambda y: y <= 1439 , wakeTimesInMins )) | |
# if wakeTimes has only one time entry and it is invalid | |
if not wakeTimesInMins: | |
return "Wrong time entry. Please enter a time between 00:00 to 23:59." | |
# if wakeTimes has only one time entry and it is valid | |
if len(wakeTimesInMins) == 1: | |
return wakeTimesInMins[0] | |
# else if wakeTimes has more than one valid time entry | |
else: | |
minDiff = 1439 | |
for t1,t2 in tuple(combinations(wakeTimesInMins, 2)): | |
if abs(t1 - t2) < minDiff: | |
minDiff = abs(t1 - t2) | |
return minDiff | |
if __name__ == "__main__": | |
print(minDifference([])) # return 0 | |
print(minDifference(['24:00'])) # return "Wrong time entry. Please enter a time between 00:00 to 23:59." | |
print(minDifference(['2:30'])) # return 150 | |
print(minDifference(['16:15','16:00','12:20'])) # return 15 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @meekg33k, thank you for taking the time to review and give feedback.
I think by using the inbuilt
combinations
method my solution has a complexity ofO(1)
andO(N log N)
for space and time complexity respectively.A more optimal way of solving this problem without exploring all the combinations would be: