user3091216
user3091216

Reputation: 85

Bad value returned by calculation

This function is ment to sum all of the numbers that are in an even index of the list, and then multiply this sum by the last number of the list.

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]




def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for i in array:
            x = array.index(i)
            if (x % 2 == 0):
                sum_array += int(i)
                print (sum_array)
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0


checkzi(checkio)

the 'print' output I get is: -37 -56 -27 -24 -88 -52 -26 29 -36 -36 .

By this I can understand that the last number that was added correctly was 55. after 55, 84 wasn't added correctly. More to that, the final sum that I get is -1476, while it is suppose to be 1968.

I can't find any reason for this. not something I can see anyway.

Any idea anyone?

Thanks!!

Upvotes: 0

Views: 40

Answers (2)

Henry Keiter
Henry Keiter

Reputation: 17168

array.index() will always return the first index at which a value is found. So you're looping through every element, and then looking to see what index it's at--but if there are duplicate elements (which there are), then you only see the index of the first one, leading you to always add (or always exclude) that number whenever you encounter it.

A much cleaner (and quicker) way to do this is to only iterate over the even elements of the list in the first place, using Python's slice notation:

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    sum_array = 0
    for value in array[::2]: #loop over all values at even indexes
        sum_array += value
    return sum_array * array[-1] # multiply by the last element in the original array

Using the built-in sum function, you could even one-line this whole thing:

def checkzi(array):
    return sum(array[::2]) * array[-1]

Upvotes: 1

Floris
Floris

Reputation: 46365

The problem is that array.index() will return the first instance of a value. You have the value 84 twice - so since the first index is odd, you never add it.

You really need to keep track of the index, not rely on uniqueness of the values. You do this with

for idx, val in enumerate(array):

now your first value will be the index, and the second value will be the value. Test idx%2==0 and you can figure it out from here.

update here is the complete code, making clear (I hope) how this works:

checkio = [-37,-36,-19,-99,29,20,3,-7,-64,84,36,62,26,-76,55,-24,84,49,-65,41]

def checkzi(array):
    if len(array) != 0:
        sum_array = 0
        for idx, x in enumerate(array):
            print "testing element", idx, " which has value ", x
            if (idx % 2 == 0):
                sum_array += x
                print "sum is now ", sum_array
            else:
                print "odd element - not summing"
        print (sum_array)
        answer = (sum_array) * (array[len(array)-1])
        return (answer)
    else:
        return 0

checkzi(checkio)

Output:

testing element 0  which has value  -37
sum is now  -37
testing element 1  which has value  -36
odd element - not summing
testing element 2  which has value  -19
sum is now  -56
testing element 3  which has value  -99
odd element - not summing
testing element 4  which has value  29
sum is now  -27
testing element 5  which has value  20
odd element - not summing
testing element 6  which has value  3
sum is now  -24
testing element 7  which has value  -7
odd element - not summing
testing element 8  which has value  -64
sum is now  -88
testing element 9  which has value  84
odd element - not summing
testing element 10  which has value  36
sum is now  -52
testing element 11  which has value  62
odd element - not summing
testing element 12  which has value  26
sum is now  -26
testing element 13  which has value  -76
odd element - not summing
testing element 14  which has value  55
sum is now  29
testing element 15  which has value  -24
odd element - not summing
testing element 16  which has value  84
sum is now  113
testing element 17  which has value  49
odd element - not summing
testing element 18  which has value  -65
sum is now  48
testing element 19  which has value  41
odd element - not summing
48

You obviously want to take the print statements out - I added them to help explain the program flow.

Upvotes: 0

Related Questions