-2

I found this solution on a problem on edabit, but I cant get my head around it.

def count_overlapping(intervals, point):
    return sum(min(x)<=point<=max(x) for x in intervals)

This is the problem text

Create a function that takes in a list of intervals and returns how many intervals overlap with a given point.

An interval overlaps a particular point if the point exists inside the interval, or on the interval's boundary. For example the point 3 overlaps with the interval [2, 4] (it is inside) and [2, 3] (it is on the boundary).

John Popa
  • 23
  • 4
  • Please update your question with a specific part you are having trouble with. Also, please check the syntax. I'm sure there is a missing bracket somewhere. – quamrana Jul 16 '20 at 16:00

1 Answers1

1

This isn't really the right place to ask this, but I'll answer it anyway.

def count_overlapping(intervals, point):
    return sum(min(x)<=point<=max(x) for x in intervals)

Let's pull it apart. What it does is:

lst = [min(x)<=point<=max(x) for x in intervals]
return sum(lst)

Which you can rewrite like:

lst = []
for x in intervals:
    if min(x) <= point <= max(x):
        lst.append(True)
    else:
        lst.append(False)
return sum(lst)

The reason this works is because True counts as a 1 and False counts as a 0.

Nathan
  • 3,558
  • 1
  • 18
  • 38