Skip to content

Instantly share code, notes, and snippets.

@hyeonseok
Created Oct 22, 2013
Embed
What would you like to do?
<?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