[Tutor] Algorithm Question

Lloyd Hugh Allen lha2@columbia.edu
Wed, 09 Jan 2002 18:09:31 -0500


It seems to me that we're looking for a variation on a multiplication
table (only with addition, and with funny headings on the rows and
columns). A vanilla multiplication table (so that you can see what's
going on) can be generated by

>>> thelist = []
>>> for count1 in range(11):
	thelist.append([])
	for count2 in range(11):
		thelist[count1].append(count1*count2)

to get an addition table, change the * to a +; to get what you want,
give count1 and count2 a coefficient in the last line, as in

		thelist[count1].append(5*count1 + 2*count2)

as a mathematician, I'm uncomfortable with getting rid of the entries
generated by count1 being 0 and those generated by count2 being 0 (the
generators should be in your answer list/matrix; to do otherwise makes
my stomach twist for some reason).

The exercise for the reader is to take this 2-dimensional list/matrix
and flatten it into a one-dimensional list with no repeated entries. I
had one lying around in lisp a second ago, and I guess it's not that
hard to write a cdr() and car()...

or you could leave out the line "    thelist.append([])" and scratch the
index from the last line, and then you only have to sort and kill
repeated entries. I think it looks nicer as a table though. Aesthetics.
Depends on what you need this for.

Kirby Urner wrote:
> 
> At 06:59 AM 1/9/2002 -0800, Kirby Urner wrote:
> >At 03:42 PM 1/9/2002 +0100, Remco Gerlich wrote:
> >>On  0, Kirby Urner <urnerk@qwest.net> wrote:
> >> > I modified this to sort the output, and gave it:
> >> >
> >> >    >>> sums([5,11,15])
> >> >
> >> > and got:
> >> >
> >> > [31, 36, 41, 42, 46, 47, 51, 52, 53, 56, 57, 58, 61, 62,
> >> > 63, 64, 66, 67, 68, 69, 71, 72,
> >> >
> >> > etc. etc.
> >> >
> >> > but shouldn't 5 + 2*15 = 30 be on the list?  I confess
> >> > I never understood Daniel's description of what he needed
> >> > very well.
> >
> >Sorry, I goofed, I meant 5 + 2*15 = 35.  Shouldn't 35 be
> >present?
> 
> Nevermind, same answer -- you're saying 11 has to be there.
> OK.  I got it now.  I see what you're telling me.
> 
> Kirby
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor