[ create a new paste ] login | about

Link: http://codepad.org/AGTiPoy1    [ raw code | output | fork ]

gre - Python, pasted on Feb 4:
#!/usr/bin/env python


def column1(exc):
    c = 0
    for letter in exc.upper():
        c = 26 * c + (ord(letter) - 64)
    return c


def column2(exc):
    return reduce(lambda c, l: 26 * c + (ord(l) - 64), exc.upper(), 0)


def excel1(col):
    e = []
    while col > 0:
        col -= 1
        r = col % 26
        col //= 26
        e.append(chr(r + 65))
    return "".join(e[::-1])


def excel2(col):
    ds = []
    while col > 0:
        col, d = divmod(col - 1, 26)
        ds.append(d)
    return "".join(chr(d + 65) for d in ds[::-1])


from itertools import imap


def test_excel():
    print all(imap(lambda i: column2(excel2(i)) == i, xrange(1, 2 ** 16)))
    return None


if __name__ == "__main__":
    for col in [1, 26, 27, 256]:
        print excel1(col)
        print excel2(col)
    for exc in ["A", "Z", "AA", "IV"]:
        print column1(exc)
        print column2(exc)
    test_excel()


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
A
A
Z
Z
AA
AA
IV
IV
1
1
26
26
27
27
256
256
True


Create a new paste based on this one


Comments: