[Tutor] compare

Liam Clarke ml.cyresse at gmail.com
Mon Oct 31 12:18:03 CET 2005


> >
> >1 # calc.py
> >    2 def calc(seq):
> >    3   maximum = 0
> >    4   max_item = []
> >    5   for i in seq:
> >    6     product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
> >    7     if product > maximum:
> >    8        maximum = product
> >    9        max_item = i
> >   10     elif product == maximum:
> >   11        max_item += ','+i
> >   12   return max_item, maximum
> >   13
> >   14 seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
> >   15 max_item, maximum = calc(seq)
> >   16 print "Maximum at", max_item, ",product", maximum

Well, it runs and returns [5, 6, 7, 9, 8], 55566.

Is that what you were expecting? If you can tell us what output you
expected and how it deviated, it's always good, details are A Good
Thing. ;-)

Try feeding it seq = [ [5,6,7,8,9], [5,6,7,8,9] ] and  you'll get the
error Johan described, as product will equal maximum. Are you wanting
the output

Maximum at [ [5,6,7,8,9], [5,6,7,8,9] ] ,product 50463? As it looks
like you're trying to join two lists.

I think you've been bitten by some operator overloading wherein
>>> [1,2,3] + [4,5,6]
[1, 2, 3, 4, 5, 6]

Basic rule is, you can use a + between string and string,
integer/float and integer/float, list and list and tuple and tuple,
and "," is a string, so it breaks it. Take it out...

max_item += i #( max_item = max_item + i)

At this point, max_item would equal [5,6,7,8,9] and adding i to it
would produce -
[5,6,7,8,9, 5,6,7,8,9].

You've got various ways to do this.

max_item = [max_item] + [i] #and this is a terrible way to do it,
max_item = [max_item, i] #Better. There's a problem with this however...

Or, you could change line 8 and 11 to
max_item.append(i)

But that would give [[5,6,7,8,9]] if product != maximum the second
time around, but
[[5,6,7,8,9], [5,6,7,8,9]] if product did equal maximum.

The problem with this - max_item = [max_item, i] is that if you entered
seq = [[5,6,7,8,9], [5,6,7,8,9], [5,6,7,8,9] ]

max_item would come out as [ [ [5,6,7,8,9], [5,6,7,8,9] ],[5,6,7,8,9] ]...

If you are going to be handling more than two sequences, I'd recommend
that you change lines 8 and 11 to max_item.append(i) and insert a line
or two before your return statement on line 12, and it would all look
like this -

def calc(seq):
    maximum = 0
    max_item = []
    for i in seq:
        product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
        if product > maximum:
            maximum = product
            max_item.append(i) #append is a list method, by the way
        elif product == maximum:
            max_item.append(i)

#This will remove [[5,6,7,8,9]]
if len(max_item) == 1:
    max_item = max_item[0]

return max_item, maximum


Do you see how that works? max_item = [max_item] + [i]  would give you
the same multiplying brackets problem as max_item = [max_item, i],
plus it's mentally harder to debug.

Especially as i is traditionally used as an integer  in 'for loops'
since the days of BASIC...
for i in range(10):
    print x[i]

so having [i] sitting out there by itself is confusing.

Good luck.

Regards,

Liam Clarke


More information about the Tutor mailing list