Sparrow Algorithm From Date Intersection to Thinking Mode

date intersection

As early as about 13 years ago, I made a system with very simple functions. I made a schedule management function. There is a small knowledge point involved here, which is the intersection of dates. What I want to share with you today is a topic that starts from this point and takes the intersection of time periods.

Business logic

The logic is very simple, it is to take the intersection of one or two date segments, but if it is implemented according to normal thinking, in fact, we can exhaustively list the scenarios of the intersection of dates, there are about 4 situations

    s1--------------e1
        s2-----------------e2
        s1--------------e1
    s2-----------e2
        
    s1-----------------e1
        s2-------e2
         s1-------e1
    s2-----------------------e2

If we implement our expression according to this logic, we may need to write at least 4 kinds, the logic is complex and the readability is poor

Reverse Thinking

This is a very important mindset in software development and is highlighted in Programmer Mathematics. The programmer's mathematics mentioned here is a set of books, including basic mathematics of programming, probability theory and linear code. It is also the basic theory of entering machine learning. Interested friends can buy it and read it. Let's practice reverse thinking with an example. In fact, according to the logic of intersection, there are 4 cases listed above. But we think in turn that there are only two possibilities in the case of no intersection

                s1-------------e1
    s2------e2
    s1------e1
                s2--------------e2

We set the dates of the two time periods as s1(start)-e1(end) and s2-e2, and the dequeuing expression can be deduced by reverse derivation

Here we assume

s1<e1 & s2<e2

=>

e2<s1||e1<s2

It means that it is impossible to generate an intersection if this condition is met, and the above expressions are translated into business language

The first time period has ended, the second time period has not yet started

By taking the negation of expressions, that is, all cases where there is an intersection

!(e2<s1||e1<s2)

=>

e2>=s1&&e1>=s2

This can be used as the final expression for the intersection of time

Here is a very important point, this time period we have conditions

s1<e1 & s2<e2

This is a sufficient condition for the expression to hold

question

Here we just deduce the expression of time intersection. Let's expand the proposition. At present, we only take the intersection of two time periods. What if we take the intersection of n time periods?

      ---------   ---------------
   ---------    ---   ---
      -------------   -------
    ------     -----------
       ---------    ------------
  • How to deal with hundreds of thousands of such time periods?
  • What if we need to group by time periods with date intersection?
  • What if our business wasn't dates, but other data types? How to abstract the computational model? Can non-date data also be grouped?

Please follow the next blog

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326119038&siteId=291194637