[OT] Reciprocal function?

Anton Vredegoor anton at vredegoor.doge.nl
Fri Nov 22 17:39:09 EST 2002


On Fri, 22 Nov 2002 13:06:04 -0500, "Terry Reedy" <tjreedy at udel.edu>
wrote:

<snip some concerns about unorthodox approaches>

Today I got a visualization that solved the problem (I think). It uses
the matrix that the code at the bottom produces. Until today it didn't
occur to me to describe its structure like this:

def blockinfo(block,base):
    blockstart,width = 0,1
    for i in range(block):
        width = width * base
        blockstart = blockstart + width
    return blockstart, width

It should be relatively easy to construct a function that finds a
value in a block (and each value occurs only once in a block) and
returns it's row and column headers.

The code computes a matrix where the individual cells are defined as
the output of "concatting" the row and column headers.

Thank you very much for your reaction and I would welcome further
pointers,

regards,
	Anton.

def rowinc(x,base):
    i,j = 0,1
    while i <= x:
        j = j * base
        i = i + j
    return j
        
def concat(a,b,base):
    f = rowinc(b,base)
    return f*(a+1)+b
  
def pprint(m):
    w = len(m[0][0])
    for x in m:
        for y in x:
            fs = '%%%is' %w
            print fs %y,
        print
  
def main():
    base = 2
    x1,y1,x2,y2 = 0,0,20,10
    width = 7
    res = []
    tmp = [' ' * width]
    for i in range(y1,y2+1):
        tmp.append(i)
    res.append(tmp)
    for i in range(x1,x2+1):
        tmp=[i]
        for j in range(y1,y2+1):
            tmp.append(concat(i,j,base))
        res.append(tmp)
    pprint(res)
   
if __name__=='__main__':
    main()




More information about the Python-list mailing list