Created
November 4, 2011 05:19
-
-
Save k-holy/1338716 to your computer and use it in GitHub Desktop.
Excel列名変換問題 via http://d.hatena.ne.jp/JunichiIto/20111102/1320253815
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Holy\Example; | |
require_once realpath(__DIR__ . '/lime.php'); | |
$conv = function($string) { | |
$i = 0; | |
return array_sum(array_map(function($char) use (&$i) { | |
$num = strpos('ABCDEFGHIJKLMNOPQRSTUVWXYZ', $char) + 1; | |
$num *= pow(26, $i); | |
$i++; | |
return $num; | |
}, str_split(strrev($string)))); | |
}; | |
// Test case | |
$t = new \lime_test(); | |
// 問題1 | |
$t->is( 1, $conv('A')); | |
$t->is( 2, $conv('B')); | |
$t->is( 3, $conv('C')); | |
$t->is( 25, $conv('Y')); | |
$t->is( 26, $conv('Z')); | |
$t->is( 27, $conv('AA')); | |
$t->is( 28, $conv('AB')); | |
$t->is( 51, $conv('AY')); | |
$t->is( 52, $conv('AZ')); | |
$t->is( 53, $conv('BA')); | |
$t->is( 54, $conv('BB')); | |
$t->is( 256, $conv('IV')); | |
$t->is(16384, $conv('XFD')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Holy\Example; | |
require_once realpath(__DIR__ . '/lime.php'); | |
class Converter | |
{ | |
const TO_NUMBER = 0; | |
const TO_ALPHA = 1; | |
public static $map = array(); | |
public function __construct() | |
{ | |
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
self::$map = array_combine(range(1, strlen($alphabet)), str_split($alphabet)); | |
} | |
public function convert($mode, $value) | |
{ | |
return ($mode == self::TO_NUMBER) ? $this->toNumber($value) : $this->toAlpha($value); | |
} | |
public function toNumber($string) | |
{ | |
$num = 0; | |
$length = count(self::$map); | |
foreach (str_split(strrev($string)) as $i => $char) { | |
$num += intval(array_search($char, self::$map)) * pow($length, $i); | |
} | |
return $num; | |
} | |
public function toAlpha($num) | |
{ | |
$string = ''; | |
$length = count(self::$map); | |
while ($num >= 1) { | |
$key = $num % $length; | |
$num = $num / $length; | |
if ($key === 0) { | |
$key = $length; | |
$num--; | |
} | |
$string .= self::$map[$key]; | |
} | |
return strrev($string); | |
} | |
} | |
// Test case | |
$conv = new Converter(); | |
$t = new \lime_test(); | |
// 問題1 | |
$t->is( 1, $conv->toNumber('A')); | |
$t->is( 2, $conv->toNumber('B')); | |
$t->is( 3, $conv->toNumber('C')); | |
$t->is( 25, $conv->toNumber('Y')); | |
$t->is( 26, $conv->toNumber('Z')); | |
$t->is( 27, $conv->toNumber('AA')); | |
$t->is( 28, $conv->toNumber('AB')); | |
$t->is( 51, $conv->toNumber('AY')); | |
$t->is( 52, $conv->toNumber('AZ')); | |
$t->is( 53, $conv->toNumber('BA')); | |
$t->is( 54, $conv->toNumber('BB')); | |
$t->is( 256, $conv->toNumber('IV')); | |
$t->is(16384, $conv->toNumber('XFD')); | |
// 問題2 | |
$t->is( 1, $conv->convert(0, 'A')); | |
$t->is( 2, $conv->convert(0, 'B')); | |
$t->is( 3, $conv->convert(0, 'C')); | |
$t->is( 25, $conv->convert(0, 'Y')); | |
$t->is( 26, $conv->convert(0, 'Z')); | |
$t->is( 27, $conv->convert(0, 'AA')); | |
$t->is( 28, $conv->convert(0, 'AB')); | |
$t->is( 51, $conv->convert(0, 'AY')); | |
$t->is( 52, $conv->convert(0, 'AZ')); | |
$t->is( 53, $conv->convert(0, 'BA')); | |
$t->is( 54, $conv->convert(0, 'BB')); | |
$t->is( 256, $conv->convert(0, 'IV')); | |
$t->is(16384, $conv->convert(0, 'XFD')); | |
$t->is('A' , $conv->convert(1, 1)); | |
$t->is('B' , $conv->convert(1, 2)); | |
$t->is('C' , $conv->convert(1, 3)); | |
$t->is('Y' , $conv->convert(1, 25)); | |
$t->is('Z' , $conv->convert(1, 26)); | |
$t->is('AA' , $conv->convert(1, 27)); | |
$t->is('AB' , $conv->convert(1, 28)); | |
$t->is('AY' , $conv->convert(1, 51)); | |
$t->is('AZ' , $conv->convert(1, 52)); | |
$t->is('BA' , $conv->convert(1, 53)); | |
$t->is('BB' , $conv->convert(1, 54)); | |
$t->is('IV' , $conv->convert(1, 256)); | |
$t->is('XFD', $conv->convert(1, 16384)); |
問題2もやってみたけど仕様が「ただし、問題1で作ったプログラムを拡張すること。」だったので1から書き直した(ノ∀`)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lime_testいれた^o^