0

In Find all possible combinations that overlap by end and start, we get the following program that gets all possible combinations of ranges that only overlap by end and start values. The output is given in string format, as with the following example:

def getAllEndOverlappingIndices(lst, i, l):
    r = -1
    if i == len(lst):
        if l:
            print(l)
        return

    n = i + 1
    while n < len(lst) and r > lst[n][0]:
        n += 1
    getAllEndOverlappingIndices(lst, n, l)

    n = i + 1
    r = lst[i][1]
    while n < len(lst) and r > lst[n][0]:
        n += 1
    getAllEndOverlappingIndices(lst, n, l + str(lst[i]))

indices = [(0.0, 2.0), (0.0, 4.0), (2.5, 4.5), (2.0, 5.75), (2.0, 4.0), (6.0, 7.25)]
indices.sort()

print(getAllEndOverlappingIndices(indices, 0, ''))
(6.0, 7.25)
(2.5, 4.5)
(2.5, 4.5)(6.0, 7.25)
(2.0, 5.75)
(2.0, 5.75)(6.0, 7.25)
(2.0, 4.0)
(2.0, 4.0)(6.0, 7.25)
(0.0, 4.0)
(0.0, 4.0)(6.0, 7.25)
(0.0, 2.0)
(0.0, 2.0)(6.0, 7.25)
(0.0, 2.0)(2.5, 4.5)
(0.0, 2.0)(2.5, 4.5)(6.0, 7.25)
(0.0, 2.0)(2.0, 5.75)
(0.0, 2.0)(2.0, 5.75)(6.0, 7.25)
(0.0, 2.0)(2.0, 4.0)
(0.0, 2.0)(2.0, 4.0)(6.0, 7.25)

I would like to have the output of the function be a list of lists with each sublist holding the separated tuples (currently, strings of the tuples are being returned). For the example given above, the output would instead be L = [[(6.0, 7.25)], [(2.5, 4.5)], [(2.5, 4.5), (6.0, 7.25)], ..., [(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]. I tried creating a list at beginning of the function and appending each call of the function, but I believe I am appending strings of the tuples instead of the tuples themselves. Is it possible to change the output of this function to what I would like as described? I don't have much experience with recursion and would appreciate any tips.

Luke Poeppel
  • 143
  • 1
  • 10

1 Answers1

1

Right now, what the code does is create a giant string because you start with a string and cast every tuple into a string before concatenating everything together. Instead, what you can do is pass in an empty list and add tuples to the list until you finish your combination. Once you get to the end of the combination, add it to a global array that holds all your combinations.

# Create a global array to hold all your results.
results = []

def getAllEndOverlappingIndices(lst, i, l):
    r = -1
    if i == len(lst):
        if l:
            # Instead of printing final combination, add the combination to the global list
            results.append(l)
        return

    n = i + 1
    while n < len(lst) and r > lst[n][0]:
        n += 1
    getAllEndOverlappingIndices(lst, n, l)

    n = i + 1
    r = lst[i][1]
    while n < len(lst) and r > lst[n][0]:
        n += 1
    # Wrap the tuple in the list to take advantage of python's list concatenation
    getAllEndOverlappingIndices(lst, n, l + [lst[i]])

indices = [(0.0, 2.0), (0.0, 4.0), (2.5, 4.5), (2.0, 5.75), (2.0, 4.0), (6.0, 7.25)]
indices.sort()
# Pass in an empty list here instead of an empty string
getAllEndOverlappingIndices(indices, 0, [])

Output:

[[(6.0, 7.25)], [(2.5, 4.5)], [(2.5, 4.5), (6.0, 7.25)], [(2.0, 5.75)], [(2.0, 5.75), (6.0, 7.25)], [(2.0, 4.0)], [(2.0, 4.0), (6.0, 7.25)], [(0.0, 4.0)], [(0.0, 4.0), (6.0, 7.25)], [(0.0, 2.0)], [(0.0, 2.0), (6.0, 7.25)], [(0.0, 2.0), (2.5, 4.5)], [(0.0, 2.0), (2.5, 4.5), (6.0, 7.25)], [(0.0, 2.0), (2.0, 5.75)], [(0.0, 2.0), (2.0, 5.75), (6.0, 7.25)], [(0.0, 2.0), (2.0, 4.0)], [(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]

Output editted for visiblity:

[[(6.0, 7.25)],
[(2.5, 4.5)],
[(2.5, 4.5), (6.0, 7.25)],
[(2.0, 5.75)],
[(2.0, 5.75), (6.0, 7.25)],
[(2.0, 4.0)],
[(2.0, 4.0), (6.0, 7.25)],
[(0.0, 4.0)],
[(0.0, 4.0), (6.0, 7.25)],
[(0.0, 2.0)],
[(0.0, 2.0), (6.0, 7.25)],
[(0.0, 2.0), (2.5, 4.5)],
[(0.0, 2.0), (2.5, 4.5), (6.0, 7.25)],
[(0.0, 2.0), (2.0, 5.75)],
[(0.0, 2.0), (2.0, 5.75), (6.0, 7.25)],
[(0.0, 2.0), (2.0, 4.0)],
[(0.0, 2.0), (2.0, 4.0), (6.0, 7.25)]]
Lapis Rose
  • 644
  • 3
  • 15
  • Hey, @Lapis Rose. Thanks for your help on the above! As you might see here https://stackoverflow.com/questions/62734114/iterative-solution-to-end-overlapping-indices, I'm trying to get an iterative solution that will generate the same output, but I'm having trouble. Do you have any ideas on how this could be done? – Luke Poeppel Jul 05 '20 at 04:11