Created
February 17, 2014 12:38
-
-
Save kazua/9049833 to your computer and use it in GitHub Desktop.
世界で闘うプログラマ本の1-5 by PHP
This file contains 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 | |
//write kazua | |
//世界で闘うプログラマ本1-5 by PHP | |
//test | |
$a = new worldproblem1_5(); | |
echo $a->zip("kkkksssllllllllllllssssaaaaaaaaa").PHP_EOL; | |
echo $a->zip("sssswwwweeeddfffffff").PHP_EOL; | |
echo $a->zip("aweyerhhhhhhferyerrrrrrr").PHP_EOL; | |
echo $a->zip("sdfsdfsdgrgegeh").PHP_EOL; | |
class worldproblem1_5{ | |
function zip($str){ | |
$cnt = 1; | |
$bf = $str[0]; | |
$zstr = ""; | |
for($i =1;$i < strlen($str);$i++){ | |
if($str[$i] === $bf) $cnt++; | |
else{ | |
$zstr .= $bf.$cnt; | |
$bf = $str[$i]; | |
$cnt = 1; | |
} | |
} | |
if (strlen($str) < strlen($zstr.$bf.$cnt)) return $str; | |
else return $zstr.$bf.$cnt; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment