Comparing elements in a list in Python's for -loop

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)

Upvotes: 1

Views: 1761

Answers (5)

John La Rooy
John La Rooy

Reputation: 304137

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)

Upvotes: 1

John La Rooy
John La Rooy

Reputation: 304137

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

Upvotes: 1

Robert Rossney
Robert Rossney

Reputation: 96702

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.

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992917

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 

Upvotes: 2

Jack Lloyd
Jack Lloyd

Reputation: 8405

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.

Upvotes: 2

Related Questions