Checking for overlap between time spans

后端 未结 5 2107
别跟我提以往
别跟我提以往 2021-01-06 01:23

I have a list of time entries (HHMM format) with a start time and a stop. I\'m having trouble figuring out how to code it in Python where it returns if there\'s an overlap o

5条回答
  •  北荒
    北荒 (楼主)
    2021-01-06 02:16

    Simple way to do it:

    I change the number into string since entry 3 contains 0900, which is invalid.

    entry01 = ('1030', '1245')
    entry02 = ('1115', '1300')
    
    entry03 = ('0900', '1030')
    entry04 = ('1215', '1400')
    
    def check(entry01, entry02):
        import itertools
        input_time_series = list(itertools.chain.from_iterable([entry01, entry02]))
        if input_time_series != sorted(input_time_series):
            return False
        return True
    
    >>> check(entry01, entry02)
    False
    >>> check(entry03, entry04)
    True
    

提交回复
热议问题