-2

Getting error:

File "inversions.py", line 26, in merge
if left[i] < right[j]:
TypeError: 'int' object is not subscriptable

My implementation of merge sort is like so; accepts a list and it's length. Base case is when length is 1, where I simply return the list (not as an int, but as a list):

def mergesort(arr, length):
    if length == 1:
        return arr

Working of merge sort function in non-base-case situation:

    n = length // 2
    left = arr[:n]
    right = arr[n:]

    lsort = mergesort(left, len(left))
    rsort = mergesort(right, len(right))
    result = merge(lsort, rsort, length)

    return result

Then there's the merge function for merging two sorted sub-lists, defined like so:

def merge(left, right, length):
    buff = []
    i = j = count = 0

This merge function is obviously called by the merge sort function after all recursive calls are done.

There are if-else statements in this merge function that handle it's working:

    if left[i] < right[j]:
        buff.append(left[i])
        i += 1

        if i == len(left):
            for j in range(j, len(right)):
                buff.append(right[j])
            break

    elif left[i] > right[j]:
        buff.append(right[j])
        j += 1

        count += len(left) - i

        if j == len(right):
            for i in range(i, len(left)):
                buff.append(left[i])
            break

In the end, this merge function returns 'count'; number of inversions.

Judging from the error, it seems that 'left' etc are being interpreted as integers, thus giving me the subscript error. But I just can't understand WHY they are ints when clearly they should be lists (or maybe I'm just missing something very obvious).

I just can't get my head around this. Any and all help is appreciated! :)

  • 4
    post your code here – Jean-François Fabre Jan 25 '17 at 16:57
  • Paste the reorganize code here directly; as text. If it's long, you need to produce a minimal example that demonstrates your problem. – Carcigenicate Jan 25 '17 at 16:57
  • Sorry! New here. I've edited it. Thanks :) – Pranjal Verma Jan 25 '17 at 17:12
  • Your code snippet doesn't show actual assignments to local names. With no assignments you cannot determine where integers are assigned to names `left` and `right`. Please show [MCVE]. – Łukasz Rogalski Jan 25 '17 at 17:13
  • _In the end, this merge function returns 'count'; number of inversions._ it smells like you may trying to use return value of this function somewhere where actual sequence is expected. – Łukasz Rogalski Jan 25 '17 at 17:17
  • @Łukasz Rogalski I added more info! 'actual sequence is expected' hmm but I thought the variable 'result' would just take on the int value as opposed to the list it takes usually! Tbh I did just copy my implementation of merge sort and tried modifying it! – Pranjal Verma Jan 25 '17 at 17:22
  • They are not "interpreted" as integers, they actually *are* integers. The issue, then is where your assignment to the variable is assigning an integer rather than a list. Python does not "interpret" your data, unlike say php. – dsh Jan 25 '17 at 18:12
  • @dsh Yes, you're right! I did realise my mistake in the code. Thanks for the reply! :) – Pranjal Verma Jan 25 '17 at 18:22

1 Answers1

0

I've seen your code, and in merge function you're returning the variable count of type int and then passing it to merge again as array in the recursion. So the block if left[i] < right[j]: won't work.

Change merge function to return buff variable.

  • oh sh*t yeah! Thanks a lot I just realised it, how I'm failing all the recursion steps. I was imagining just a 2 level recursion tree while writing this and I never thought of the recursions in the middle! – Pranjal Verma Jan 25 '17 at 17:26
  • You're welcome. I think this [link](http://stackoverflow.com/questions/19072004/understanding-the-recursion-of-mergesort) will help you visualize the process. – Leonardo Braga Jan 25 '17 at 17:36
  • oh, amazing! Thanks again haha! :) – Pranjal Verma Jan 25 '17 at 17:59