1

I have this array: [[0,10,20],[1,15,25],[2,20,40]]

0, 1, 2 would be ids and 10,20 15,25 20,40 would be their respective range.

So, I need to figure out the ranges and which ids has which range.

Each id would represent a flashlight, and each one has its own range, I need this to figure out which areas are being illuminated by which flashlights.

I don't know if was clear enough, so here is what I expect to be returned from the code:

[
 [0],[10,15],
 [0,1],[15,20],
 [1,2],[20,25],
 [2], [25,40]
]

Thanks in advance.

3
  • 4
    Any attempts on this challenge so far? Commented Apr 12, 2013 at 4:57
  • I'm confused about exactly what you need to do. What does the output represent? Also, do you need to have the 0, 1, 2 indexes inside the array? You can index the ranges with those numbers already without having them there. Commented Apr 12, 2013 at 5:05
  • I will edit to explain better. Commented Apr 12, 2013 at 5:16

2 Answers 2

2

First, the real line must be segmented:

min_boundaries = set(min for id, min, max in orig)
max_boundaries = set(max for id, min, max in orig)
segment_boundaries = sorted(list(min_boundaries | max_boundaries))

Then, for each segment, check which lights illuminate that segment. A point in the middle of the segment is enough:

result = []
old_boundary = segment_boundaries[0]
for boundary in segment_boundaries[1:]:
    bounds = [old_boundary, boundary]
    middle = (old_boundary + boundary) / 2
    ids = [id for id, min, max in orig if min < middle < max]
    result.append(ids)
    result.append(bounds)
    old_boundary = boundary
Sign up to request clarification or add additional context in comments.

Comments

0

With some changes in the data structure here is the simple solution:

adict = {0: [10, 20], 1: [15, 25], 2: [20, 40]}
data = [(10, 15), (15, 20), (20, 25), (25, 40)]

# This will check the membership 
# in_([3,4], [2,3]) returns True
def in_(val1 , val2):
    return val2[0] >= val1[0] and val2[1] <= val1[1]


res = {}
for each in keys:
    for key in adict.keys():
        if in_(adict[key], each):
            res.setdefault(each, []).append(key)

O/P

>>>res
{(10, 15): [0], (15, 20): [0, 1], (25, 40): [2], (20, 25): [1, 2]}

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.