Wednesday, July 13, 2016

excel column name for n

n is 0 based. This works.
 0  a
25  z
26 aa
51 az
52 ba

 n  %  i
52  0  0 a
 2  2  1 b
 0

 n  %  i
51 25 25 z
 1  1  0 a
 0

// n is 0-based
(n):
    if n < 0:
        throw
    buffer = new
    first = true
    do:
        i = n %26
        if !first:
            i--
        first = false
        buffer.append(alphabets[i])
        n = n /26
    while n > 0
    return buffer.reverse().tostring()
This is cleaner.
// n is 0-based
(n):
    if n < 0:
        throw
    buffer = new
    while n >= 0:
        i = n %26
        buffer.append(alphabets[i])
        n = n /26 -1
    return buffer.reverse().tostring()
[Hat tip to D]

No comments:

Post a Comment