Skip to content

Instantly share code, notes, and snippets.

@hyeonseok
Created October 22, 2013 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyeonseok/7093757 to your computer and use it in GitHub Desktop.
Save hyeonseok/7093757 to your computer and use it in GitHub Desktop.
<?php
function tests($expect, $actual) {
if ($expect == $actual) {
echo('PASS');
} else {
echo('FAIL');
}
echo(': ' . $expect . '/' . $actual . PHP_EOL);
}
function get_index($num) {
$n = $num;
$result = '';
while ($n > 0) {
$r = ($n % 26 == 0) ? 26 : $n % 26;
$result = chr(65 + $r - 1) . $result;
$n = ($n - $r) / 26;
}
return $result;
}
tests('A', get_index(1));
tests('B', get_index(2));
tests('O', get_index(15));
tests('T', get_index(20));
tests('Y', get_index(25));
tests('Z', get_index(26));
tests('AA', get_index(27));
tests('AB', get_index(28));
tests('ZZ', get_index(702));
tests('AAA', get_index(703));
tests('ACB', get_index(756));
tests('XFD', get_index(16384));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment