Comment detail

与えられた文字列でピラミッド (Nested Flatten)

	
1
2
3
4
5
6
7
8
<?php
function pyramid($str){
	for($i=$j=$n=strlen($str); $i>0; $i--, $n=$j*2-$i)
		printf("% {$n}s\n", trim(join(" ", preg_split("//", substr($str, $i-1, $j)))));
}

pyramid("abracadabra");
?>
ちょっと修正。
trim取ったり、substrの引数取ったり。
1
2
3
4
5
6
7
8
<?php
function pyramid($str){
	for($i=$n=strlen($str); $i>=0; $i--, $n=strlen($str)*2-$i+1)
		printf("% {$n}s\n", join(" ", preg_split("//", substr($str, $i))));
}

pyramid("abracadabra");
?>

Index

Feed

Other

Link

Pathtraq

loading...