Advertisement
tsvtln

Untitled

Feb 8th, 2025
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.31 KB | Source Code | 0 0
  1. from json import loads
  2. from datetime import datetime
  3.  
  4.  
  5. class EventOverlapCheck:
  6.     @staticmethod
  7.     def checker(evt: str) -> str:
  8.         evt_dict, events_checked, overlaps = {}, [], []
  9.         evt_list = evt.split(', ')
  10.  
  11.         # we'll store the events into a mapping table (dictionary) for faster lookup
  12.         for e in evt_list:
  13.             e_name, l_events = e.split(' = ')  # we get the event name and it's times
  14.             evt_dict[e_name] = loads(l_events)  # using json loads to convert the stringed lists into proper lists
  15.  
  16.         dict_size = len(
  17.             evt_dict)  # getting the lenght of the dictionary for use with a loop and having a dynamic name inside
  18.  
  19.         # we'll convert the times from H:M to total minutes for easier comparison and store them in a list
  20.         for i in range(1, dict_size + 1):
  21.             event_name = f"event_{i}"
  22.             if event_name in evt_dict:
  23.                 event_start, event_end = evt_dict[event_name]
  24.                 start_minutes, end_minutes = datetime.strptime(event_start, "%H:%M").hour * 60 + datetime.strptime(
  25.                     event_start, "%H:%M").minute, datetime.strptime(event_end, "%H:%M").hour * 60 + datetime.strptime(
  26.                     event_end, "%H:%M").minute
  27.                 events_checked.append((start_minutes, end_minutes, event_name))
  28.  
  29.         # we sort the list so we can break the loop earlier and avoid unnecessary runs and check them in
  30.         # chronological order
  31.         events_checked.sort()
  32.  
  33.         # we'll go through every event in the sorted list and compare it with every other event to check for overlaps
  34.         for i in range(dict_size): # get the current event to compare with the next events in (j)
  35.             for j in range(i + 1, dict_size):
  36.                 # we unpack the event in comparison (i) with values sN(start time), eN(end time), evtN{event_(i) name)
  37.                 s1, e1, evt1 = events_checked[i]
  38.                 s2, e2, evt2 = events_checked[j]
  39.                 # we check if the end of i is less than the start of j, to avoid further checks and exit the loop early
  40.                 if e1 < s2:
  41.                     break
  42.                 # we get the max of start from i and j and the min of i and j so we can get the
  43.                 # period which both events are active at the same time
  44.                 overlap_start, overlap_end = max(s1, s2), min(e1, e2)
  45.  
  46.                 # here we call the helper function to get the H:M format as striing
  47.                 start_time = EventOverlapCheck.minutes_to_time_str(overlap_start)
  48.                 end_time = EventOverlapCheck.minutes_to_time_str(overlap_end)
  49.                 # and we append the information to be returned to the user
  50.                 overlaps.append(f"{evt1} overlaps with {evt2} from {start_time} to {end_time}")
  51.  
  52.         return ("true\n" + "\n".join(overlaps)) if overlaps else "false"
  53.  
  54.     @staticmethod
  55.     def minutes_to_time_str(minutes: int) -> str:
  56.         # this is a helper function to convert back minutes into HH:MM format
  57.         return f"{minutes // 60:02d}:{minutes % 60:02d}"
  58.  
  59.  
  60. if __name__ == '__main__':
  61.     # we get the events from input as per example format:
  62.     # Input: event_1 = ["02:15","04:00"], event_2 = ["04:00","06:00"]
  63.     events = input('Please enter events:\n')
  64.     print(EventOverlapCheck().checker(events))
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement