0

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!!

user3091216
  • 85
  • 2
  • 3
  • 13
  • Could you please fix the indentation? As it is currently displayed the `else:` is not in the right place. Indentation is obviously crucial for Python, so I would not want to presume I know how you had indented your code. – Floris Jun 11 '14 at 14:49
  • Yes i'm sorry, I fixed it – user3091216 Jun 11 '14 at 14:53

2 Answers2

1

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]
Community
  • 1
  • 1
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
0

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.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • No I do not think that "for i in array" returns an index and that's why I added after that the statement: x = array.index(i) more so, Yes, I am multiplying the last element, please read the question. – user3091216 Jun 11 '14 at 14:58
  • Thanks for response Floris, But if I have a for-loop wont it iterate through the 2nd 84 too? how can this be solved? – user3091216 Jun 11 '14 at 15:03
  • See updated answer for solution. It iterates through the values, but doesn't find the right index with your approach. The approach I suggest gives you both index and value without the need for a lookup. – Floris Jun 11 '14 at 15:05
  • I'm sorry, but I do not understand this statement: "for idx, val in enumerate(array)". what is idx? and I don't understand the function so I wont figure it out... could you explain some more? – user3091216 Jun 11 '14 at 15:07
  • And this doesn't do any good.. I got a fully bad counting after I added this: for idx, val in enumerate(array): if (idx % 2 == 0): sum_array += int(idx) print (sum_array) – user3091216 Jun 11 '14 at 15:11
  • See my updated answer. `idx` is the index - which you use for testing odd/even. `val` (or `x` in my code sampe) is the value which you add to the sum. – Floris Jun 11 '14 at 15:14