[Tutor] [cross product instead of nested loops]

Julieta Rangel julieta_rangel@hotmail.com
Sun, 06 May 2001 22:29:24 -0500


Thank you.  Now it makes sense.  By the way, I want to apologize in case you 
received this message over and over.  What happened is that every time I 
tried to send it I received a message that the e-mail could not be 
delivered, so I would send it again, and again.  Obviously this message 
could be delivered.  I must have been annoying posting the message over and 
over.  Sorry!

Julieta


>From: Daniel Yoo <dyoo@hkn.eecs.berkeley.edu>
>To: Julieta Rangel <julieta_rangel@hotmail.com>
>CC: sheila@thinkspot.net, tutor@python.org
>Subject: Re: [Tutor] [cross product instead of nested loops]
>Date: Sun, 6 May 2001 19:55:06 -0700 (PDT)
>
>On Sun, 6 May 2001, Julieta Rangel wrote:
>
> > I've been playing with the cross product definition you gave me. It 
>makes a
> > lot of sense, and I'm sure it will make it a lot easier for me to prove
> > associativity; however, I'm doing something wrong because when I run it, 
>I
> > don't get the result I thought I would.  Would you take a look and tell 
>me
> > what I'm doing wrong?
> >
> >
> > def cross(set1,set2):
> >     resulting_set = []
> >     for s1 in set1:
> >         for s2 in set2:
> >             resulting_set.append( (s1, s2) )
> >             return resulting_set                 ## <-- bug!
>
>There's a bug here: we want to return the resulting_set only after we
>finish going through both for loops.  That is:
>
>###
>def cross(set1,set2):
>     resulting_set = []
>     for s1 in set1:
>         for s2 in set2:
>             resulting_set.append( (s1, s2) )
>     return resulting_set
>###
>
>The difference is in indentation, but the idea is that the request about
>returning a result should be outside of the looping.  After fixing this,
>the program should work ok.
>

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com