qunayu
qunayu

Reputation: 1197

value of an index not changed to what its supposed to (line 9)

This is the entire code in python3:

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))

The problem occurs on line 9. Not sure how to categorise this problem, but I can't for the life of me figure out what's missing here. Thanks in advance.

Upvotes: 0

Views: 34

Answers (2)

Sarath Sadasivan Pillai
Sarath Sadasivan Pillai

Reputation: 7091

The problem

The problem is with assigning temp as array

With temp = array, you don't actually have two lists. The assignment just copies the reference to the list, not the actual list, so both temp and array refer to the same list after the assignment.

temp = array
for i in range(0, len(temp)):
    if temp[i] >= 1:
        print(temp)
        temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
        array[i] = array[i] - temp[i]

    array = temp

In the above code, change to temp[i] changes array[i], So

  temp[i] = 1                       # sets temp[i] = array[i]  = 1
  array[i] = array[i] - temp[i]     # array[i] = tempa[i] = 1-1

Hence temp[i] is 0 instead of 1

How to solve

You may use copy ,deepcopy or slicing to copy the array to temp

Here is the updated code that works ,used slicing

Code

def rainwater(array):
    max_value = max(array)
    count = 0
    for a in range(0, max_value):
        temp = array[:]
        for i in range(0, len(temp)):
            if temp[i] >= 1:
                print(temp)
                temp[i] = 1 # somehow this line always changes temp[i] to 0 instead of 1
                array[i] = array[i] - temp[i]

            array = temp[:]

        for i in range(0, len(temp)):
            if temp[i] == 1:
                del temp[:i]
                break
        for i in range(0, len(temp) - 1, -1):
            if temp[i] == 1:
                del temp[i:]
                break
        if len(temp) <= 1:
            return count
        for i in range(0, len(temp)):
            if temp[i] == 0:
                count = count + 1
    return count


# driver program -> should return 6

print(rainwater([0,1,0,2,1,0,1,3,2,1,2,1]))

Upvotes: 1

swbandit
swbandit

Reputation: 2006

This line sets your value to 0:

array[i] = array[i] - temp[i]

And the reason is array and temp are the same objects. So x - x is always going to be zero.

I think that you're assuming that this code on line 5 makes a copy of the array:

temp = array

But that is not true. This makes both temp and array reference the same array. If you actually want a copy of the array, do this:

temp = array[:]

Also, you may want to fix line 11 as well, if you are making a copy.

I would recommend that you read more about passing variables by reference and by value.

Upvotes: 1

Related Questions