[Tutor] unsubscribe

Raphael Volz volz at aifb.uni-karlsruhe.de
Mon Feb 23 06:02:48 EST 2004


unsubscribe

> -----Ursprüngliche Nachricht-----
> Von: tutor-bounces at python.org 
> [mailto:tutor-bounces at python.org] Im Auftrag von Gregor Lingl
> Gesendet: Montag, 23. Februar 2004 12:01
> An: alice
> Cc: tutor at python.org
> Betreff: Re: [Tutor] global variables & recursion
> 
> 
> Hi Alice! (Hi Robin Hood?)
> 
> alice schrieb:
> 
> >I would like some advice.
> >  
> >
> I'll try. As your second problem, determioning s and t is a 
> generaliziation of  the simplecomputation of gcd, I'll 
> concentrate on  this.  (Although  
> there
> are 'famous' versions of euclids algorithm which are well 
> worth discussing. So if you are interested in it,  feel fre 
> to ask more ...)
> 
> >Is there a better way of doing this?
> >Particularly I'm not sure if using global variables is a good idea.
> >  
> >
> Generally you should avoid using global variables, if it can 
> easily be done.
> 
> ># Find integers s and t such that as + bt = gcd(a,b):
> >
> ># global variables (is this bad programming style?)
> >  
> >
> A standard way to do so is passing them as parameters with 
> default arguments.
> 
> >## _eulen = 0  # recursion depth
> >## _rem = []   # list of remainders
> >
> >def st(a,b,_eulen=0, _rem=[]):
> >
> >##    global _eulen
> >##    global _rem
> >
> >    # a should be bigger than b:
> >    if (b > a):
> >       a, b = b, a
> >
> >    # find remainder:
> >    r = a % b
> >
> >    if (r == 0):        # finished
> >
> >       # find s and t:
> >       s = 1
> >       t = 1 - (a / b)
> >       for i in range(_eulen):
> >           b = _rem.pop()
> >           a = _rem.pop()
> >           s,t = t, s - (a/b)*t
> >
> >       # show results:
> >       print "gcd(%d,%d) = (%d)(%d) + (%d)(%d) = %d" % 
> > (a,b,a,s,b,t,a*s+b*t)
> >
> >       # reset _eulen and _rem:
> >##       _eulen = 0
> >  
> >
> ## Interestingly it is not necessary to reset :eulen, as it 
> is set to 0 ## when st() is defined, and cannot be changed, 
> because integers are ## immutable python objects ## the 
> contrary is true for _rem, which as a list is a mutable python 
> object *and*
> ## will be changed during the run of this version of your 
> function. ## (This issue is a bit tricky)
> 
> >       _rem = []
> >
> >    else:              # not finished.
> >
> >       # we will need this information later:
> >##       _eulen += 1  ## will be passed as argument (see below)
> >       _rem.append(a)
> >       _rem.append(b)
> >
> >       # function calls itself:
> >       st(b,r,_eulen+1)
> >  
> >
> ## Here _rem need not be inserted as argument as it si the 
> default value 
> of this
> ## fourth parameter.
> 
> I'll show you some more 'amendments in a follow up posting
> 
> Regards, Gregor
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org 
> http://mail.python.org/mailman/listinfo/tutor
> 




More information about the Tutor mailing list