PHPで言語処理100本ノック 2015(第2章:後半)

www.cl.ecei.tohoku.ac.jp

15. 末尾のN行を出力

<?php
$fileRows = explode("\n", file_get_contents($argv[1]));
for($i = (count($fileRows)-1) - $argv[2]; $i < (count($fileRows)-1); $i++) {
    echo $fileRows[$i]."\n";
}

確認

$ tail -n 7 hightemp.txt

16. ファイルをN分割する

<?php
$fileRows = explode("\n", file_get_contents($argv[1]));
$contents = '';
$fileNameCount = 0;
for($i = 0; $i < count($fileRows); $i++) {
    $contents .= ($fileRows[$i]."\n");
    if (($i % $argv[2]) === ($argv[2] - 1)) {
        file_put_contents('./16_'.$fileNameCount.'.txt', $contents, LOCK_EX);
        $contents = '';
        $fileNameCount++;
    }
}

確認

$ split -l 3 hightemp.txt 16_sh_

17. 1列目の文字列の異なり

<?php
$result = [];
$fileRows = explode("\n", file_get_contents($argv[1]));
foreach ($fileRows as $row) {
    $columns = explode("\t", $row);
    if ($columns[0] === '') {
        break;
    }
    $result = array_merge($result, [$columns[0] => 0]);
}
var_dump(array_keys($result));

確認

$ cut -f 1 hightemp.txt | sort -k 1,1 | uniq

18. 各行を3コラム目の数値の降順にソート

<?php
$result = [];
$rows = explode("\n", file_get_contents($argv[1]));
foreach ($rows as $row) {
    if ($row === '') {
        break;
    }
    $col3 = explode("\t", $row)[2];
    if (count($result) === 0) {
        $result[] = $row;
        continue;
    }
    foreach ($result as $index => $value) {
        if (explode("\t", $value)[2] < $col3) {
            array_splice($result, $index, 0, $row);
            break;
        }
        if (count($result) - 1 === $index) {
            $result[] = $row;
        }
    }
}
var_dump($result);

確認

$ sort -k 3,3 -r hightemp.txt

19. 各行の1コラム目の文字列の出現頻度を求め,出現頻度の高い順に並べる

<?php
$result = [];
$rows = explode("\n", file_get_contents($argv[1]));
foreach ($rows as $row) {
    if ($row === '') {
        break;
    }
    $col1 = explode("\t", $row)[0];
    if (count($result) === 0 || !array_key_exists($col1, $result)) {
        $result[$col1] = 0;
        continue;
    }
    foreach ($result as $key => $value) {
        if ($key === $col1) {
            $result[$col1] = $result[$col1] + 1;
        }
    }
}
arsort($result);
var_dump($result);

確認

$ cut -f 1 hightemp.txt | sort -k 1,1 | uniq -c | sort -nr