Sergey Filkin
Sergey Filkin

Reputation: 485

Strange behaviour on a list with 100000 elements

I have joined Standford online course on Algorithms design and now I'm solving the first programming question.

The file contains all the 100,000 integers between 1 and 100,000 (including both) in some random order(no integer is repeated). Your task is to find the number of inversions in the file given (every row has a single integer between 1 and 100,000). Assume your array is from 1 to 100,000 and i-th row of the file gives you the i-th entry of the array.

Update: I have found that my code works only for the 2^n case. Problem is in the code, not Python. I have updated the code, now it work fine and I have done the quiz. Thanks to all who helped

Fixed code is:

 def merge_count_split (a, b):
        c = []
        inv = 0
        i=0
        j=0
        for k in range( len(a) + len(b) ):
                if i < len(a) and j < len(b):
                        if a[i] < b[j]:
                                c.append(a[i])
                                i += 1
                        elif a[i] > b[j]:
                                c.append(b[j])
                                inv += len(a)-i
                                j += 1
                elif i == len(a):
                        c.append(b[j])
                        j += 1
                elif j == len(b):
                        c.append(a[i])
                        i += 1
        return c, inv

def count_inv (data):
        n = len(data)
        if n == 1:
                return data, 0
        a, x = count_inv(data[:n/2])
        b, y = count_inv(data[n/2:])
        c, z = merge_count_split(a,b)
        return c, x + y + z

with open('IntegerArray.txt') as f:
        array = [int(line) for line in f]
        print count_inv(array)[0]

This program works fine for small arrays, but for the large array from the question it prints array of 65536 numbers in proper order, not 100000, as I expect. It omits numbers at random places.

What is the reason for this unexpected behaviour of python?

Upvotes: 2

Views: 2960

Answers (1)

senderle
senderle

Reputation: 150987

By setting n = len(a) and merging only n * 2 times, you truncate b if it's longer than a.

This partially explains the striking fact that you have 2 ** 16 items in the resulting list.

Upvotes: 2

Related Questions