Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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? -
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' -
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”. -
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? -
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 -
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.
A prize to the first person to correctly tell me what the function is for.
joke/meme