-1

I am trying the following code

def getCount(mySet):
    l = len(mySet)
    hsh={}
    lSet=[]
    rSet=[]
    for i in range(0,l-1):
        for j in range(i+1,l):
            if ((mySet[i]+mySet[j])%k==0):
                lSet = mySet
                rSet = mySet
                print "i, j ", i,j
                del lSet[i]
                del rSet[j]
                if len(lSet)==0 or len(rSet==0):
                    return 0
                left = getCount(lSet)
                right = getCount(rSet)
                if left==right:
                    return left

    return len(lSet)

n,k = [int(i) for i in raw_input().split(" ")]

mySet=[]

for x in raw_input().split(" "):
    mySet.append(int(x))

print getCount(mySet)

where an example input is

5 5
2 4 1 6 8

however, I am getting the error

del rSet[j]
IndexError: list assignment index out of range

I don't understand why this is. How is j out of range?. In the line above, I printed out

i, j  0 4
user5739619
  • 1,748
  • 5
  • 26
  • 40

1 Answers1

1

I don't understand why this is. How is j out of range?. In the line above, I printed out

That is because, the moment you delete lSet[i], the same element gets deleted from rSet too! And this happens because both the variables lSet and rSet refer to the same variable. To create a copy, you would do:

lSet = mySet[:]
rSet = mySet[:]
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104