[Tutor] help

Jonathon Sisson sisson.j at gmail.com
Fri Aug 25 02:14:04 CEST 2006


Gah!

Seems I sent that reply before stepping through my code a bit...
The last section of that code should be:

else:    # kl and ll contain the same number here
    both.append(kl[indexK])
    if indexK < indexL:     # this block is needed so we
        indexL = indexL + 1 # don't enter an endless loop...
    else:
        indexK = indexK + 1

The way I had it set up before (see below) always increments indexL.
Not a problem for this particular instance, but definitely shabby 
programming...oops...

Jonathon


Hi Mike,

I'm not sure I'm understanding exactly what it is you're looking for
here, but from what I can tell you're looking for something such as:

num(220, 330) #for example
input a range of 10 # again, for example

which would result in an output of 0, 660, 1320 and 1980 (because
these four numbers are common to both kl and ll).

If this is indeed the case, then you need to replace the "while f<c"
statement block in your code.  Your code is looking for "f" (i.e. 0 -9
for the example numbers above), in kl and ll.  This explains why you
can get the least common multiple (0), but nothing more.  The solution
would be  to replace the entire "while f<c" block with this:

    while (indexK < c and indexL < c):
        if kl[indexK] < ll[indexL]:        # kl contains a smaller number
            indexK = indexK + 1
        elif ll[indexL] < kl[indexK]:     # ll contains a smaller number
            indexL = indexL + 1
        else:    # kl and ll contain the same number here
            both.append(kl[indexK])
            if kl[indexK] < ll[indexL]: # this block is needed so we
                                        # don't enter an endless loop...
                indexK = indexK + 1
            else:
                indexL = indexL + 1

(make sure to declare indexK=indexL=0 somewhere before this block)
The following "print both" statement would then output:

[0, 660, 1320, 1980]

Is this what you were asking for?

Jonathon


mike viceano wrote:

> > hello i need a little help with a common multiple module i am haveing
> > problems with the best i can get it is to show the least common multiple i
> > think the problem is eather the and command ot the if command
> >
> >    # finds common multiples
> > def nums(a,b): # assigns numbers to a and b
> >     print "enter range"
> >     c = input(">")
> >     both=[]
> >     e=-1
> >     f=-1
> >     kl=[]
> >     ll=[]
> >     while e<c:
> >         e=e+1
> >         k=a*e
> >         l=b*e
> >         print a,"X",e,"=",k,"   ",b,"X",e,"=",l
> >         kl.append(k)
> >         ll.append(l)
> >     while f<c:
> >         f=f+1
> >         if f in kl and f in ll:
> >             both.append(f)
> >     print both
> >
> > any help would be great
> >
> >
> >
> > ^_^"  s33 y4
> >
> > _________________________________________________________________
> > Call friends with PC-to-PC calling -- FREE  
> > http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
>   
-------------- next part --------------
An embedded message was scrubbed...
From: Jonathon Sisson <sisson.j at gmail.com>
Subject: Re: [Tutor] help
Date: Thu, 24 Aug 2006 18:46:32 -0500
Size: 3080
Url: http://mail.python.org/pipermail/tutor/attachments/20060824/b50e9427/attachment-0001.mht 


More information about the Tutor mailing list