[Fwd: [Tutor] global variables & recursion]

Gregor Lingl glingl at aon.at
Wed Feb 25 06:42:32 EST 2004


alice intended this to go to the list

-------- Original-Nachricht --------

Oh yes, very nice.
That's much nicer than my original attempt.
Is it always possible to turn a recursive algorithm into an iterative one?
(sorry if that's a stupid question)

On Wednesday 25 February 2004 00:44, Gregor Lingl wrote:
> Karl Pflästerer schrieb:
> >ACK.  After having looked at your second solution I tried a solution
> >which makes it IMO clearer how s and t are found (in the original code
> >it was a bit hidden IMO).
> >
> >def gcd (a, b):
> >    a, b = abs(a), abs(b)
> >    rem = []
> >    while b > 0:
> >            rem.append((a,b))
> >            a, b = b, a%b
> >    return a, rem
> >
> >def st (a, b):
> >    gc, rem = gcd(a, b)
> >    rem.reverse()
> >    s = t = 1
> >    for a, b in rem:
> >       s, t = t, s - (a//b)*t
> >    return gc, (a, s), (b, t)
>
> That's a fine solution....
> mine was directly derived from alice's,  using her  recursive approach
> and (intentionally) making only slight modifications. Certainly  alice
> will be pleased
> to see  her  solution  now turned into a iterative one.







More information about the Tutor mailing list