PPCGユーザー名をゴルフダウン


32

チャット行っコメントとそれに続く会話に触発されて、この挑戦​​をしました。

この辺りのイニシャルで言及されているのは私だけですか?私たちはすべてゴルフをすることについてです。MBとD-nobがあり、... O.

私が「CH」として知られているなら、他の誰もが同様に初期ベースのニックネームを持つべきだと思います。

以下は、プログラミングパズルとコードゴルフのトップ100ユーザーの評価リストです。

Martin Büttner
Doorknob
Peter Taylor
Howard
marinus
Dennis
DigitalTrauma
David Carraher
primo
squeamish ossifrage
Keith Randall
Ilmari Karonen
Quincunx
Optimizer
grc
Calvin's Hobbies
ugoren
Mig
gnibbler
Sp3000
aditsu
histocrat
Ventero
xnor
mniip
Geobits
J B
Joe Z.
Gareth
Jan Dvorak
isaacg
edc65
Victor
steveverrill
feersum
ace
Danko Durbić
xfix
PhiNotPi
user23013
manatwork
es1024
Joey
daniero
boothby
nneonneo
Joey Adams
Timwi
FireFly
dansalmo
grovesNL
breadbox
Timtech
Flonk
algorithmshark
Johannes Kuhn
Yimin Rong
copy
belisarius
professorfish
Ypnypn
trichoplax
Darren Stone
Riot
ProgramFOX
TheDoctor
swish
minitech
Jason C
Tobia
Falko
PleaseStand
VisioN
leftaroundabout
alephalpha
FUZxxl
Peter Olson
Eelvex
marcog
MichaelT
w0lf
Ell
Kyle Kanos
qwr
flawr
James_pic
MtnViewMark
cjfaure
hammar
bitpwner
Heiko Oberdiek
proud haskeller
dan04
plannapus
Mr Lister
randomra
AShelly
ɐɔıʇǝɥʇuʎs
Alexandru
user unknown

これは私がそれを得た方法です

チャレンジ

文字列のリストを取り込んで、最小の一意の初期ベースのニックネームの文字列の別のリストを出力するプログラムまたは関数を作成し、リストの先頭に近いものを優先します。

ニックネームを作成するために、指定された順序でリスト内の各文字列Sにこのメソッドを適用します。

  1. Sをスペースで区切られた単語に分割し、プロセス内のすべてのスペースを削除します。
  2. Sの単語の最初の文字の文字列の空でないプレフィックスを、最短から最長までリストします。
    例えばJust Some NameJJSJSN
  3. このリストで、Sのニックネームとして既に選択されているニックネームと同一ではない最初のアイテムを選択します。ニックネームが選択された場合は停止し、そうでない場合はステップ4に進みます。
    たとえばJust Some Name、最初の文字列の場合J、ニックネームが保証されます。
  4. 接頭辞を再度リストしますが、今回は自然な場所に最初の単語の2番目の文字を含めます。
    例えばJust Some NameJuJuSJuSN
  5. このリストに対して手順3と同じ操作を行い、一意のニックネームが見つかったら停止します。
  6. 最初の単語の残りの文字でこのプロセスを繰り返し、最終的に2番目の単語に文字を挿入し、次に3番目の単語に挿入して、一意のニックネームが見つかるまで続けます。
    ここに記載されている例えば最初の一意の文字列は、ニックネームになります:
    JusJusSJusSN
    JustJustSJustSN
    JustJustSoJustSoN(注o後に追加されませんでしたJust
    JustJustSomJustSomN
    JustJustSomeJustSomeN
    JustJustSomeJustSomeNa
    JustJustSomeJustSomeNam
    JustJustSomeJustSomeName

最終的に、すべての入力文字列は、一意のニックネーム(文字列と同一である可能性があります)で終わる必要があります。このメソッドを使用して、入力文字列のいずれも同じニックネームにマップされないと想定できます。

私の間違いを修正するために更新されました!

入力用

Martin Buttner
Doorknob
Peter Taylor
Howard
marinus
Dennis
DigitalTrauma
David Carraher
Martin Bitter
Martin Butter
Martin Battle
Martini Beer
Mart Beer
Mars Bar
Mars Barn

ニックネームは

M
D
P
H
m
De
Di
DC
MB
Ma
MaB
Mar
MarB
Mars
MarsB

詳細

  • 入力は、ファイル(行ごとに1つの名前)、stdin /コマンドライン経由で一度に1つの名前、または文字列のリストの関数引数として、または名前の間に改行がある単一の文字列の関数引数として指定できます。
  • 出力はstdout(行ごとに1つのニックネーム)に出力されるか、文字列のリストとして、またはニックネーム間に改行がある1つの文字列として関数によって返される必要があります。
  • 理想的には、プログラムは行末記号以外の文字を含む名前に対して機能します。ただし、すべての名前に印刷可能なASCIIのみが含まれていると想定できます。(PPCG名はそうではありません。)
  • 通常のスペース文字のみが単語の区切りとしてカウントされます。先頭と末尾のスペースは無視できます。

得点

バイト単位の最短提出勝ちです。Tiebreakerは最も早く投稿された回答に進みます。


49
これは、私が夜中に目が覚めた理由を説明します。
マーティンエンダー

回答:


8

CJam、58 53バイト

これはたくさんのゴルフができます。

LqN/{:Q1<aQ,,:)QS/f{{1$<_,@-z1e>}%W<s}+{a1$&!}=a+}/N*

コード拡張

L                         "Put an empty array on stack. This is the final nickname array";
 qN/{  ...   }/           "Read the input and split it on new lines. Run the block for each";
     :Q1<a                "Store each name in Q and get its first char. Wrap it in an array";
          Q,,:)           "Get an array of 1 to length(name) integers";
               QS/        "Split the name on spaces";
f{{           }%   }      "for each of the integer in the array, run the code block";
                          "and then for each of the name part, run the inner code block";
   1$<                    "Copy the integer, take first that many characters from the";
                          "first part of the name";
      _,@-z1e>            "Get the actual length of the part and the number of characters";
                          "to be taken from the next name part, minimum being 1";
                W<        "Get rid of the last integer which equals 1";
                  s       "Concat all name parts in the array";
                    +     "Add the list of nick names as per spec with the first character";
{     }=                  "Get the first nick name that matches the criteria";
 a1$&                     "Wrap the nick name in an array and do set intersection with";
                          "the copy of existing nick names";
     !                    "Choose this nick name if the intersection is empty";
N*                        "After the { ... }/ for loop, the stack contains the final";
                          "nick names array. Print it separated with new lines";

こちらからオンラインでお試しください


2
OPへの私のコメントを参照してください:「Ju」または「Jus」が「Just Some Name」、「Maertin Butter」の有効なニックネームである場合、「Ma」、「MaB」、「Mar」、「MarB」になります。
edc65

@ edc65の言うことは本当です。ごめんなさい 必要に応じて、物事を変更しないようにします。それは私の間違いでした。
カルバンの趣味

9

JavaScript(ES6)159

例ではなく仕様に従ってください。

現在の中間の単語(最初は最初の単語)を持つニックネーム候補を生成します。現在の前の単語は「そのまま」使用されます。現在の後の単語は、文字なしで、または最初の文字だけで貢献します。現在の単語は、ループごとにさらに1文字追加されます。

例'ただ、いくつかの名前' => 'だけ'、 '一部'、 '名前'
CWはJust、位置1、してみてくださいJJSJSN
CWはJust、位置2、してみてくださいJuJuSJuSN
CWはJust、位置3、してみてくださいJusJusSJusSN
CWはJust、位置4、試すにはJustJustSJustSN
今すぐJust排出されます、SomeCw になり、位置は2から再開されます(位置1の場合、すべて試行済み)

CW Some、位置2、試してJustJustSoJustSoN
CW Some、位置3、試してJustJustSomJustSomN
CW Some、位置4、試してJustJustSomeJustSomeN
Some疲れている、NameCWとなり、2に再スタート位置

CWはName、位置2、試してJustJustSomeJustSomeNa
CWはName、位置3、してみてくださいJustJustSomeJustSomeNam
CWはName、位置4、してみてくださいJustJustSomeJustSomeName
それはすべての人々です!

コード

(qは現在の単語位置、pはスライス位置です)

F=l=>
  l.map(w=>{ 
    for(w=w.match(/[^ ]+/g),q=p=0;
        w.every((w,i)=>~o.indexOf(t+=i<q?w:i>q?w[0]:w.slice(0,p+1)),t='')
        &&(w[q][p++]||(p=1,w[++q]));
       );
    o.push(t)
  },o=[])&&o

Firefox / FireBugコンソールでテストする

F(['Martin Buttner','Doorknob','Peter Taylor','Howard','marinus'
  ,'Dennis','DigitalTrauma','David Carraher'
  ,'Martin Bitter','Martin Butter','Martin Battle','Martini Beer','Mart Beer'])

[「M」、「D」、「P」、「H」、「m」、「De」、「Di」、「DC」、「MB」、「Ma」、「MaB」、「Mar」、「 MarB "]


2

PHP、327 289 275 274 270

まだゴルフの可能性が少しあるかもしれません。

while($n=fgets(STDIN)){$c=count($w=preg_split('/\s+/',trim($n)));$p=[];for($k=0;$k<$c;$p[$k]++){for($t='',$j=0;$j<$c;$j++)$t.=substr($w[$j],0,$p[$j]+1);for($j=1;$j<=strlen($t);$j++)if(!in_array($v=substr($t,0,$j),$u))break 2;$k+=$p[$k]==strlen($w[$k]);}echo$u[]=$v,'
';}
  • プログラムはstdin / stdoutで動作し、ASCIIで動作し、UTFでバグがあります
  • 使用法: php -d error_reporting=0 golfnicks.php < nicknames.txt
  • またはcat <<EOF | php -d error_reporting=0 golfnicks.php+名前のリスト+EOF
  • Webブラウザーで関数としてテストするには、内訳を取得し、でマークされたすべての行のコメントを解除し// FUNC、でマークされた行をコメント化し//PROGます。試してf(array_fill(0,21,'Just Some Name'));

壊す

#error_reporting(0);function f($a){echo'<pre>'; // FUNC
#foreach($a as$n) // FUNC
while($n=fgets(STDIN)) // PROG
{
    $c=count($w=preg_split('/\s+/',trim($n)));     // split name to words, count them
    $p=[];                                         // initialize cursors
    for($k=0;$k<$c;$p[$k]++)
    {
        for($t='',$j=0;$j<$c;$j++)$t.=substr($w[$j],0,$p[$j]+1); // concatenate prefixes
        for($j=1;$j<=strlen($t);$j++)              // loop through possible nicks
            if(!in_array($v=substr($t,0,$j),$u))   // unused nick found
                break 2;                           // -> break cursor loop
        $k+=$p[$k]==strlen($w[$k]);                // if Cw exhausted -> next word
        // strlen()-1 would be correct; but this works too)
    }
    echo$u[]=$v,'
';
}
#echo '</pre>';} // FUNC
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.