Entry 963

Will's make_path problem

   

Submitted by Rodney on Aug. 26, 2008 at 12:58 a.m.
Language: Python. Code size: 1.3 KB.

# Number of subdirectories within a directory depends on the depth
# here the topmost directory has 64 subdirectories, with that
# number cut in half at each increase in depth, but with a minimum
# of 2 subdirectories
def dirsInDirOnLevel(lvl):
    didol = 64 >> lvl
    return 2 if didol < 2 else didol

def filesInDirOnLevel( lvl ):
    return 100 - dirsInDirOnLevel(lvl)

def dirsAtLevel( lvl ):
    return 1 if lvl == 0 else dirsAtLevel(lvl-1)*dirsInDirOnLevel(lvl-1)

def filesOnLevel(lvl):
    return  dirsAtLevel(lvl) * filesInDirOnLevel(lvl)


def make_path(n):
    lvl = 0
    ftl = {}
    while 1:
        fol = filesOnLevel(lvl)
        if n <= fol: break
        n -= fol
        lvl += 1

    dn,fn = divmod(n-1, filesInDirOnLevel(lvl))
    fp = "f%d"%(fn+1)
    while (lvl > 0):
        lvl -= 1
        dn,fn = divmod( dn, dirsInDirOnLevel(lvl))
        fp = "d%d/%s"%(fn, fp)
    return fp


print make_path(1), 1
print make_path(5), 5
print make_path(36), 36
print make_path(37), 37
print make_path(104), 104
print make_path(105), 105
n = 36+32*68
print make_path(n), n
n += 1
print make_path(n), n
n = 36+64*68
print make_path(n), n
n += 1
print make_path(n), n
n = 176420
print make_path(n), n
n += 1
print make_path(n), n

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).