1

Golf - シンプルなテンプレート スキームを実装します。

展開は次のとおりです。

  • %KEY% -> 値
  • %% -> %

コマンドライン引数:

  • key=valueARG1:例のようなスタイルでフォーマットされた辞書ファイル
  • ARG2: テンプレートファイル

ここで私のあまりゴルフの試み(python):261文字。

import sys
dd = dict([ll.split("=",2) for ll in open( sys.argv[1],'r') if len(ll.split("=", 2)) == 2])
tt = "".join([ ll for ll in open( sys.argv[2],'r')])
sys.stdout.write("".join([(((s == "") and "%") or ((s in dd) and dd[s]) or s) for s in tt.split("%")]))

DICT

NAME=MyName
ODDS=100

テンプレート

I, %NAME% am %ODDS% %% sure that that this a waste of time.

結果

I, My Name am 100 % sure that this is a waste of time.

はい、これは欠陥のあるテンプレートシステムであり、より短くより良い実装のために「スナップ」していることを認識しています。

4

5 に答える 5

2

Python では、組み込みの文字列フォーマットを利用して短くすることができます。構文を一致させるには、正規表現のハッキングが少し必要です。

import sys, re
sys.stdout.write(re.sub(r'%(.+?)%',r'%(\1)s',open(sys.argv[2]).read())%dict(l.split("=",2) for l in open(sys.argv[1],'r')))

139 バイトまで。(おそらく args/file-IO は実際にはゴルフ チャレンジの一部であってはなりませんか?)

于 2009-04-13T18:38:26.017 に答える
1

C#.

string s = "NAME=MyName ODDS=100"; // any whitespace separates entries
string t = "I, %NAME% am %ODDS% %% sure that that this a waste of time.";

foreach(var p in s.Split().Select(l=>l.Split('=')).ToDictionary(
e=>"%"+e[0]+"%",e=>e[1]))t=t.Replace(p.Key,p.Value);t=t.Replace("%%","%");
Console.WriteLine(t);

アルゴリズム部分は 159 だと思います。「NAME=%%」のようなものを入力すると、予期しない結果が生じる可能性があることに注意してください (%% がさらに % に折りたたまれます)。ある順序で文字列の置換を行います。

于 2009-04-13T17:34:42.427 に答える
1

vc ++で..が最速の方法かもしれません:)

void customFormat(const char* format, char dicItemSep, char dicValueSep, const char* dic, char* out)
{
    if(out)
    {
        const char x = '%';
        while(*format)
        {
            while(*format && *format != x)*out++=*format++;
        if(!*format || !*(++format))goto exit;
            if(*format == x)
            {
                *out++=x;
                continue;
            }
            const char *first = format;
            const char *d = dic;
            while(*first)
            {
                while(*d)
                {
                    while(*d && (*d != *first))d++;
                    if(*d == *first)
                    {
                        while((*first++ == *d++) && (*d != dicValueSep));
                        if(!*first)goto exit;
                        if(*d != dicValueSep || *first != x)
                        {
                            first = format;
                            while(*d && (*d != dicItemSep))d++;
                            continue;
                        }
                        break;
                    }
                }
                if(!*d)break;
                d++;
                while((*d != dicItemSep) && *d && (*out++ = *d++));
                format = ++first;
                break;
            }
        }
    exit:
        *out = 0;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    char t[1024];
    customFormat
        (
            "%NAME% am %ODDS% %% sure that that this a waste of time.",
            '\n',
            '=',
            "NAME=MyName\nODDS=100",
            t
        );
    printf("%s", t);
}
于 2009-05-12T09:35:18.530 に答える
0

Ruby、114文字

d=Hash[*[open(ARGV[0]).read.scan(/(.+)=(.*)/),'','%'].flatten]
print open(ARGV[1]).read.gsub(/%([^%]*)%/){d[$1]}
于 2009-04-28T14:56:17.923 に答える
0

質問に対する実際の回答ではありませんが、実際のタスクでこれが必要な場合、Python にははるかに読みやすいテンプレート形式があることを理解していただければ幸いです。{variable}

 # MyDict.py
 dict = {
     some : 'every',
     p : '?', 
     q : '?..'
 }

 # main file
 import MyDict
 print """
    What is {some}thing{p} 
    Who values this mysterious {some}thing{q}
 """.format(**dict)

必要に応じて、辞書形式を保持できます。

  with open(dictionary_file, 'r') as f:
      for l in f.readlines():
           k, v = '='.split(l)
           if v: dict[k] = v
于 2009-06-20T04:13:51.587 に答える