challenge 擬似lsの実装

スラッシュで区切られた文字列の配列(以下パスリスト)がある。
このパスリストにたいして擬似的なlsを行いたい。
lsはパスリストと表示対象ディレクトリのパスを入力する。

例としては以下のようになる。
pathList = ["aaa/bbb","aaa/ccc","aaa/ddd/eee","bbb/ddd/eee"]

ls(pathList,"aaa/")
>["bbb","ccc","ddd/"]

ls(pathList,"aaa/ddd/")
>["eee"]

なおパスリストが大きくなったとき、速度がなるべく低下しないように実装するのが望ましい。
文字列は任意の文字コードであると仮定してかまわない。

Posted feedbacks - D

Digital Mars D Compiler v1.015で動作確認しました。

 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
import std.stdio;
import std.string;
import std.regexp;

void ls(string[] path_list, string path)
{
    string[]    rslt;
    auto rgxp = RegExp(format("^%s([^/]+/?)", path));
    foreach (i; path_list) {
        if (-1 != rgxp.find(i)) {
            rslt ~= rgxp.match(1);
        }
    }
    string    tmp;
    foreach (i; rslt) {
        tmp ~= format("\"%s\",", i);
    }
    writef("[%s]\n", chop(tmp));
}

/*
void main(char[][] args)
{
    static string[] path_list = ["aaa/bbb", "aaa/ccc", "aaa/ddd/eee", "bbb/ddd/eee"]; 
    ls(path_list, "aaa/");
    ls(path_list, "aaa/ddd/");
}
*/

Index

Feed

Other

Link

Pathtraq

loading...