1

My respect, colleagues. I have a list of segments which may include nested segments that should be deleted.

For example we have a list (or maybe other data structure) of tuples:

[(50, 60), (10, 20), (10, 40), (40, 60), (60, 80), (75, 95), (95, 100)]

Or maybe list of ranges:

[range(50, 60), range(10, 20), range(10, 40), range(40, 60), range(60, 80), range(75, 95), range(95, 100)]

The result should be:

[(10, 40), (40, 60), (60, 80), (75, 95), (95, 100)]

enter image description here

And I must delete all nested segments.

In addition, I am not allowed to use any libraries or even import's.

Actually, I'm confused, so I'll be very grateful for any help.

6
  • Why should (50, 60), (10, 20) be deleted? What is the logic for removing elements? Commented Mar 22, 2023 at 19:48
  • ok, say you have items [(50, 60), (10, 20), (10, 100)], what should be the result? Commented Mar 22, 2023 at 19:51
  • @It_is_Chris The question, I think, is rhetorical 😉. It's just a math problem should be solved using Python. Commented Mar 22, 2023 at 19:53
  • @RomanPerekhrest I've wrote above (The result should be...) Commented Mar 22, 2023 at 19:54
  • @RomanPerekhrest [(10, 100)] Commented Mar 22, 2023 at 19:55

1 Answer 1

0

For each segment, you check if it is nested in one the others segments. If it is, you use the remove function.

segments = [(10, 40), (40, 60), (60, 80), (75, 95), (95, 100)]

segments_copy = segments.copy()

for seg_1 in segments_copy:
    for seg_2 in segments_copy:
        if seg_1 != seg_2 and seg_1[0] >= seg_2[0] and seg_1[1] <= seg_2[1]:
            segments.remove(seg_1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.