Search code examples
pythonpseudocode

I need to calculate the range of the max overlapping occurances not the max number of them


Assume that I have a dataset with numbers (start, stop):

4556745  ,  4556749
4556749  ,  5078554

… and so on

I want to make a chunk of code in order to print the range (start, stop) in which the max overlapping is occurred. Till now I have managed to calculate the max number of occurrences but not the range in which they are in.

My pseudocode – logic is like this :

maxoverlap =  zero
currentoverlap = zero
i equals zero
j equals zero
m equals len(in_mumbers) 
n equals len(out_numbers)
while (I less_than m and j less_than n):
    if (in_numbers[i] less_than out_numbers[j])
        currentoverlap equals currentoverlap + 1
        maxoverlap  equals max(maxoverlap, currentoverlap)
        i equals i + 1
    else:
        currentoverlap equals currentoverlap - 1
        j = j + 1


print maxoverlap

is there any idea, suggested readings e.t.c?


Solution

  • The maximum overlapping range is maybe (quase surely) not a whole tuple (start, stop) of input data.

    So I'd transform you all your tuple (start, stop) in range containing all the range between start and stop:

    (4556745,  4556749) → range(4556745, 4556749)
    

    and then I'll process them to count the occurrence of each number (in a dict for exemple).

    for range in ranges:
        for number in range:
            d.setdefault(num, 0)
            d[num]+=1
    

    then you can get what ever you want. To get the max occurring numbers (what you call "maximum intersection") and the number of intersections concerned, you can use something like get keys by maximum value.