tags:

views:

259

answers:

5

What is wrong in the method end in the code?

The method end returns always 1 although it should return 0 with the current data.

# return 1 if the sum of four consecutive elements equal the sum over other sum of the other three sums
# else return 0
# Eg the current sums "35 34 34 34" should return 0    

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = data.split("|");

def do_row ( arra, n ):
        return arra[4*n:4 + 4*n]

def row_summa (row):
        return sum(map(int,row))

def end ( summat ):                                     # problem here!
        equality = 1 
        for i in summat[2:5]:
                print "Comparing: ", summat[1], " and ", i, ".\n"
                if summat[1] != i:
                        equality = 0 
        print equality

for i in range(0,4):
        summat = []
        summat.append( row_summa( do_row(arra,i) ) ) 
        print row_summa ( do_row(arra,i) )
        summa = 0 

end(summat)
+2  A: 

I can't really tell what you're trying to do here, but I can certainly say why end() returns 1 instead of 0. In your last for loop, you reset summat to [] at the start of the loop, so at the end, summat only contains a single value (the one you most recently appended on). So when you ask for summat[2:5] on a list of a single item, Python returns an empty list (as there are no values in that range) - in which case there are no chances for equality to be set to zero because the loop in end never runs.

Jack Lloyd
That last `for` loop isn't part of the `end()` function.
Robert Rossney
Jack didn't claim it was. The for loop sets summat to an empty list every iteration. After the for loop has finished, end is called with summat as its argument. So, summat will only ever have one element in it: the return from row_summa(do_row(arra, i)) in the last iteration of the for loop.
jamessan
+2  A: 

I think you may have an off-by-one error. Remember that array indexes in Python start at 0, not 1. So where you do this:

   for i in summat[2:5]:
            print "Comparing: ", summat[1], " and ", i, ".\n"
            if summat[1] != i:
                    equality = 0

you are not looking at summat[0] at all. Try perhaps:

   for i in summat[1:4]:
            print "Comparing: ", summat[0], " and ", i, ".\n"
            if summat[0] != i:
                    equality = 0
Greg Hewgill
+1  A: 

First off, end doesn't return 1. It returns None. It prints 1. Kind of deceptive if you're running it from the command line.

Second, when you call end, summat is equal to [34]. So this:

for i in summat[2:5]:

never even executes. It won't do anything unless summat contains at least 3 elements.

Robert Rossney
+1  A: 

You have two problems. Initialising summat to [] inside the loop, also the off by one error Greg mentioned

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = data.split("|");

def do_row ( arra, n ):
        return arra[4*n:4 + 4*n]

def row_summa (row):
        return sum(map(int,row))

def end ( summat ):                                     # problem here!
        equality = 1 
        for i in summat[1:]:  # 1 <=== IS THE SECOND ELEMENT
                print "Comparing: ", summat[0], " and ", i, ".\n"
                if summat[0] != i:
                        equality = 0 
        print equality

summat = []        # <=== DO THIS BEFORE THE LOOP
for i in range(0,4):
        summat.append( row_summa( do_row(arra,i) ) ) 
        print row_summa ( do_row(arra,i) )
        summa = 0 

end(summat)
gnibbler
+1  A: 

You should also study this piece of code

data = "2|15|14|4|12|6|7|9|8|10|11|5|13|3|2|16"
arra = map(int,data.split("|"))
summat = [sum(arra[i:i+4]) for i in range(0,len(arra),4)]
print summat
print len(set(summat))==1
gnibbler