9

A prize to the first person to correctly tell me what the function is for.

Comments
  • 1
    Probably a dictionary word retriever
  • 1
    Looks like a clbuttic swear word filter or something similar.
  • 1
    FAOD the snippet is a unit test, the Get method is what I was asking.
  • 3
    Confusing random devs on the internet
  • 2
    Those numbers aren't indexes, those letters are encoded digits, so the Get function encodes the number as the three letters. The first letter is clearly the most significant digit.

    I got close to their numeric value by adding up their position in the alphabet bitwise shifted to the left a number of positions, but maybe there's something more to it, I don't know.

    Am I overthinking it or does it make sense?
  • 1
    @ethernetzero You're very close.
  • 3
    Assert.AreEqual("FUC", Get(4605));
  • 1
    The Get function looks like the following:

    def Get(n):
    if n < 26:
    Return char(n)
    Return Get(floor(n/26)) + char(n % 26)

    Where char (1) = 'a', char(2) = 'b', ...

    Example:
    Get(1189)=Get (1189/26)+char(1189%26)
    =Get(45)+char(19)
    =Get(1)+char(45%26)+char(19)
    =Char(1)+char(19)+char(19)
    ='a'+'s'+'s' = "ass"

    Actually, I don't know if it's correct, because if you do the mod of 26, the values range from 0 to 25, and if you map 1 to 'a', then there is no space for 'z'
  • 1
    Damn, I knew I was overthinking it. The wonders of going back to work and looking at it again when you get home: just came back and the solution popped in my head just like that, and it was so simple, man.

    It's a base 26 converter. Not sure what you use it for, though.

    A simple explanation of the conversion using Python:

    >>> divmod(1189, 26)

    (45, 19) → S, the 19th letter of the alphabet

    >>> divmod(45, 26)

    (1, 19) → S, again

    >>> divmod(1, 26)

    (0, 1) → A, the first letter

    Take them in reverse order and boom, “ASS”.
  • 1
    The implementation I have (for an input variable n) is:

    var s = "";

    while (n > 0)

    {

    var k = (n - 1) % 26 + 1;

    s = new string((char)(k + 64), 1) + s;

    n -= k;

    n /= 26;

    }

    return s;

    @FlyingBird, the line where k is set is how you get around mod mapping to 0 to 25 rather than 1 to 26.

    So the question is, why would somebody want to do this?
  • 1
    Maybe as a rudimentary way of masking IDs in URLs or something similar like generating short URLs? So, instead of:

    http://example.com/something/123456

    You'd have:

    http://examp.le/s/fzph
  • 0
    OK, time’s up. I’ll tell you. It converts a 1-based column index to the column letter code used by Excel.

    @ethernetzero, you were pretty close and persistent so you can have a prize.
  • 1
    @joycestick My cell phone is freaking out! 😆
  • 0
    @ethernetzero Have some headphones. 🎁
  • 0
    @joycestick Oh, nice, thanks!
Add Comment