Language detail: PHP

Coverage: 61.75%
number of '+' ratings
contribution for coverage

Unsolved challenges

codes

Feed

Used modules

next >>

16進数から10進数の変換 (Nested Flatten)
PHP 5.1.6

hexdec()ではinteger型の範囲をこえる数値は、float型で返すみたいなので、
gmp_strval()を使いました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// hexdec() ※うまくいかない
$ php -r 'echo hexdec($argv[1]),"\n";' 0x12437308CCB6
20080902065334
$ php -r 'echo hexdec($argv[1]),"\n";' 0x2C9C1227FC6520B
2.0090401231145E+17

// gmp_strval()
$ php -r 'echo  gmp_strval($argv[1]),"\n";' 0x12437308CCB6
20080902065334
$ php -r 'echo  gmp_strval($argv[1]),"\n";' 0x2C9C1227FC6520B
200904012311450123
ケブンッリジ関数 (Nested Flatten)

ホントは"。"や"?"を除外して考えるべきですよね?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$utf8_str = <<<HERE
こんにちは みなさん おげんき ですか? わたしは げんき です。
この ぶんしょう は いぎりす の ケンブリッジ だいがく の けんきゅう の けっか
にんげん は もじ を にんしき する とき その さしいょ と さいご の もじさえ あっていれば
じゅんばん は めちゃくちゃ でも ちゃんと よめる という けんきゅう に もとづいて
わざと もじの じゅんばん を いれかえて あります。
どうです? ちゃんと よめちゃう でしょ?
ちゃんと よめたら はんのう よろしく
HERE;
echo Cmabrigde( $utf8_str, 'UTF-8' );

function Cmabrigde( $text, $enc )
{
    $ret = '';
    $lines = explode( "\n", $text );
    foreach ( $lines as $line ) {
        $words = explode( ' ', trim($line) );
        foreach ( $words as $word ) {
            $len = mb_strlen( $word, $enc );
            if ( 3 <  $len ) {
                $head = mb_substr( $word, 0, 1, $enc );
                $body = mb_str_shuffle( mb_substr( $word, 1, $len-2, $enc ), $enc );
                $foot = mb_substr( $word, $len-1, 1, $enc );
                $word = $head.$body.$foot;
            }
            $ret .= $word.' ';
        }
        $ret = rtrim( $ret )."\n";
    }
    return $ret;
}

function mb_str_shuffle( $str, $enc )
{
    $buff = array();
    while ( $len = mb_strlen( $str, $enc ) ) {
        $buff[] = mb_substr( $str, 0, 1, $enc );
        $str = mb_substr( $str, 1, $len, $enc);
    }
    shuffle( $buff );
    return implode( '', $buff );
}
ACLの制御 (Nested Flatten)
1
2
3
4
5
<?php
$file_name = '/tmp/test.txt';
touch($file_name);
chmod($file_name, 0600);
?>
立方根の計算 (Nested Flatten)

普通に

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

function cube_root($num)
{
    $a = $num;
    
    while(true)
    {
        $b = ($num/($a*$a) + $a*2) / 3;
        ++$i;
        $diff = abs($b - $a);
        if($diff < 1e-12)break;
        $a = $b;
    }
    return $b;
}

//test code
$ret = cube_root(10);
printf("%.16f\n%.16f\n", $ret, $ret*$ret*$ret);
$ret = cube_root(100);
printf("%.16f\n%.16f\n", $ret, $ret*$ret*$ret);
ファイルサイズの取得 (Nested Flatten)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
if(file_exists($filepath)){
            $size = filesize($filepath);
            $unim = array("Bytes","KB","MB","GB","TB","PB");
            $count=0;

            while($size >= 1024 ){
                    $count++;
                    $size = $size/1024;
            }
            $size = round($size,2);
            echo $size.$unim[$count];

    }else{
            echo "not exists";
    }
}
自分自身を表示する (Nested Flatten)

初めて書いた。こういう風に書くのね。

1
2
3
4
5
<?php
$s='<?php
$s=%s%s%s;
printf($s,chr(39),$s,chr(39));';
printf($s,chr(39),$s,chr(39));
ローテートシフト (Nested Flatten)
1
2
3
4
5
6
7
8
9
<?php

function rotateShift($value, $n)
{
    return substr(str_repeat(sprintf("%016b", bindec($value)), 3), 16-$n, 16);
}

$value = "0010001111101101";
var_dump(rotateShift($value, 1));
文字列を指定されたバイト数で分割 (Nested Flatten)

とりあえずお題の消化だけ。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
//ini_set('mbstring.internal_encoding', 'Shift_JIS');
function splitBytes($string, $length, $encode)
{
    $string = mb_convert_encoding($string, $encode);
    $local_encoding = mb_internal_encoding();
    mb_internal_encoding($encode);
    
    $out = array();
    $tmp = "";
    $tmp2 = "";
    for($i = 0; $i < mb_strlen($string); $i++)
    {
        $tmp = mb_substr($string, $i, 1);
        if(strlen($tmp2.$tmp) > $length)
        {
            $out[] = $tmp2;
            $tmp2 = "";
        }
        $tmp2 .= $tmp;
    }
    $out[] = $tmp2;
    
    foreach($out as $key => $val)
    {
        $out[$key] = mb_convert_encoding($val, $local_encoding);
    }
    mb_internal_encoding($local_encoding);
    return $out;
}
$out = splitBytes("あいうえおabcdeかきくけこfghij", 10, "Shift_JIS");
var_dump($out);
$out = splitBytes("あいうえおabcdeかきくけこfghij", 10, "UTF-8");
var_dump($out);
LL Golf Hole 7 - バイト数を読みやすくする (Nested Flatten)

バグってますよ。

1
<?for($b=$argv[1];$b>1023;++$i)$b/=1024;echo$b,substr(KMGTPEZY,$i-1,!!$i);
島の数をカウントする (Nested Flatten)
PHPで書く人がいなかったので。
1.基本的に左上から右下へ捜査していく

2.特定の位置から、右、下の位置を基本的につながっているか確認する。
つながっている場合は、同じ島の番号を振る

3.途中でぐるっと回ってつながっていることがわかったらその時点で、
島の番号を振りなおして、島の数を修正する。

上下左右の4点を調べていく方法は
なんとなくありきたりなのでやめました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
$w = $h = 4;

$a = array(
      array(
        array(0,1,1,0)
       ,array(0,1,0,0)
       ,array(0,0,1,0)
       ,array(0,1,1,0)
      ),
      array(
        array(0,0,0,0)
       ,array(1,0,1,0)
       ,array(0,1,0,0)
       ,array(0,0,0,0)
      )
    );


function revSima($a0){
  global $w,$h;
    $a1 = array();
    for($i=0;$i<$w;$i++){
      for($j=0;$j<$h;$j++){
        $a1[$i][$j] = $a0[$i][$j] === 0 ? 1 : 0;
      }
    }
    return $a1;
}

function countSima($a0){
  global $w,$h;
  
  $k = array();
    $g = 1;
    
    for($i=0;$i<$w;$i++){

      for($j=0;$j<$h;$j++){
    
        if($a0[$i][$j] === 0) continue;

            if($a0[$i][$j] === 1){
              $a0[$i][$j] = ++$g;
              $k[$g] = 1;
            }
          $c = $a0[$i][$j];

          if($i + 1 < $h && $a0[$i + 1][$j] === 1){
            $a0[$i + 1][$j] = $c;
          }
          
          if($j + 1 < $w && $a0[$i][$j + 1] === 1){
            $a0[$i][$j + 1] = $c;
            
          }elseif($j + 1 < 4 && $a0[$i][$j + 1] > 1 ){

            for($m=0;$m<$i+1;$m++){
              for($n=0;$n<4;$n++){
                if($a0[$m][$n] === $c){
                  $a0[$m][$n] = $a0[$i][$j + 1];
                    }
              }
            }
            unset($k[$c]);
          }
      }
    }
    return count(array_keys($k));
}

foreach($a as $v){
  echo '(Black,White) = ('. countSima($v) . ','. countSima(revSima($v)) . ')' . "\n";
}
漢数字で九九の表 (Nested Flatten)

php5。 出題は九九なので余計ですが、出力時の桁あわせを9以上の数字にも対応しています。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$nums = array("〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一");
$start = array_search("一", $nums);
end($nums);
$digit = strlen(key($nums) * key($nums));
for ($i = $start; $i < count($nums); $i++) {
    for ($j = $start; $j < count($nums); $j++) {
        $res = str_split(str_pad($i * $j, $digit, " ", STR_PAD_LEFT));
        foreach ($res as $row) {
            if ($row == " ") {
                print(" ");
            } else {
                print($nums[$row]);
            }
        }
        print(" ");
    }
    print("\n");
}

ついでに。php4でも動作するはず。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$nums = array("〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一");
$start = array_search("一", $nums);
end($nums);
$digit = strlen(key($nums) * key($nums));
for ($i = $start; $i < count($nums); $i++) {
    for ($j = $start; $j < count($nums); $j++) {
        $res = str_pad($i * $j, $digit, " ", STR_PAD_LEFT);
        for ($k = $digit - $digit; $k < $digit; $k++) {
            $num = substr($res, $k, $start);
            if ($num == " ") {
                print(" ");
            } else {
                print($nums[$num]);
            }
        }
        print(" ");
    }
    print("\n");
}
LL Golf Hole 7 - バイト数を読みやすくする (Nested Flatten)

do_akiさんのコードをさらに圧縮してみました。75 bytes.

1
<?for($b=$argv[1];$b>=1024;++$i)$b/=1024;echo$b,substr('KMGTPEZY',$i,!!$i);

あぅ。言語選択し忘れたので、再投稿 ><

ついでにヨタまでにして、108b 短くできないかなぁ。。。

1
<?php $l=array('','K','M','G','T','P','E','Z','Y');$b=$argv[1];for(;$b>=1024;++$i){$b/=1024;}echo $b,$l[$i];
文字列型日時ののN秒後時間取得 (Nested Flatten)
1
2
3
4
<?php
function DateEx($time, $diff){
    echo echo date('YmdHis',strtotime($time)+$diff);
}
LL Golf Hole 8 - 横向きのピラミッドを作る (Nested Flatten)
1
2
for($c=2*fgets(STDIN);$i++<$c;)echo str_repeat('*',($i>$c/2)?$c-$i:$i)."
";
1
2
$n=4;
for($i=1-$n;$i<$n;$i++)printf("%'*".($n-abs($i))."s\n",'');
LL Golf Hole 6 - 10進数を2進数に基数変換する (Nested Flatten)
変換先指定対応版
1
<?$a=split(' ',fgets(STDIN));echo base_convert($a[0],10,$a[1]>0?$a[1]:2);

短くする楽しみが全く無いな。

1
<?=decbin(fgets(STDIN));
LL Golf Hole 5 - 最上位の桁を数え上げる (Nested Flatten)

powを使った方が短いようですが一応載せてみます。

1
<?for($a=0;$a<=$argv[1];)ereg('^.0*$',$a++,$m)&&print"$m[0] ";
next >>

Index

Feed

Other

Link

Pathtraq

loading...