Ununderstandable RunTime Error on the IDE


#1

I solved the problem on my local IDE with no issues, but the InterviewBit IDE is complaining when I sort the intervals array using a lambda function as the key. Below is the code and the runtime error that I get, can someone help me out and make me understand what the problem is?

def merge(self, intervals):

    intervals, i = sorted(intervals, key = lambda x: x[0]), 0
    print(intervals)
    
    while i < len(intervals) - 1:
            if intervals[i][1] >= intervals[i+1][0]:
                intervals[i] = [intervals[i][0], intervals[i+1][1]]
                intervals.pop(i+1)
    
    return intervals

Error: Traceback (most recent call last):
File “main.py”, line 186, in
result = obj.merge(v)
File “/tmp/judge/solution.py”, line 12, in merge
intervals, i = sorted(intervals, key = lambda x: x[0]), 0
File “/tmp/judge/solution.py”, line 12, in
intervals, i = sorted(intervals, key = lambda x: x[0]), 0
AttributeError: Interval instance has no attribute ‘getitem


#2

hey,i am gettiing same issue.if u get the what is the issue ,plz share .
here,we are using python-2.


#3

intervals is a list of Interval object with start and end attributes. It is in the comments.

Try:

intervals, i = sorted(intervals, key = lambda x: x.start), 0