[Tutor] Another aftermath (Stern-Brocot tree):

Gregor Lingl glingl@aon.at
Wed, 9 Jan 2002 08:25:37 +0100


Some remarks:

1. During sleep this problem apparently worked
in my brain, so when I woke up it was easy to rewrite
the code as follows - to get an equivalent iterative 
solution:

(First I added a trivial __ne__ - method to the class F)

def frac2path(f):
    path = []
    left, m, right = F(0,1), F(1,1), F(1,0)
    while f != m:
        if f < m:
            path.append("L")
            right = m
        else:
            path.append("R")
            left = m
        m = median(left,right)
        if trace: print m
    return path

def path2frac(p):
    left, result, right = F(0,1), F(1,1), F(1,0)
    while p:
        if trace: print result
        if p[0]=="L":
            result, right = median(left,result), result
        else:
            left, result = result, median(result,right)
        del p[0]
    return result

2. So it turned out to me (why only now?) that this is 
   nothing else than a special sort of binary search. 

3. Could you please explain this remark a little bit:

> As of 2.2, I think we do have class methods -- no need
> for an instance in order for the method to work.

Is the gcd in the F-class something like this?
Even when using it inside F we need a self. ... 
to call ist. And could we call this from outside
the class without an instance? 
( F.gcd(..., ...) as one might expect if it were similar
to Java, does not work. )

Sincerely
Gregor