計画とコルフ


76

#brexit#brangelinaなどのクールなtwitterハッシュタグを作成したことはありませんか?このゴルフはあなたのためです。


2つの文字列AとBを入力として受け入れ、次のアルゴリズムに従ってそれらをマージするプログラムを作成します。

  1. せてnAにおける母音基の数である(例えば、britain2つの母音基を有する:i3位とai5位に)。
    • n = 1の場合、最初の母音グループの位置からAを切り捨てます(例:bill=> b
    • n> 1の場合:A n-1番目の母音グループの位置からAを切り捨てます(例:programming=> progrbritain=> br
  2. Bの先頭のすべての子音を削除します(jennifer=> ennifer
  3. 変更されたAとBを連結します

母音はaeiou; 子音はbcdfghjklmnpqrstvwxyz

入力

入力文字列は小文字で、少なくとも1つの母音と1つの子音が含まれると仮定できます。

brad + angelina      => brangelina
britain + exit       => brexit
ben + jennifer       => bennifer
brangelina + exit    => brangelexit
bill + hillary       => billary
angelina + brad      => angelad
programming + puzzle => progruzzle
code + golf          => colf
out + go             => o

65
新しいテストケース?donald trump
スティーヴィーグリフィン

5
これらは、本質的にはportmanteausです。
mbomb007


1
@ETHproductionsこれは、次のような多くの異なる組み合わせを生成するようですDjango + Angular = Djular
-Pureferret

「n-1番目の母音グループの位置」とは何ですか
l4m2

回答:


24

ルビー、44 43 40 + 1 = 41バイト

-pフラグ用に+1バイト。STDINでスペースで区切られた入力を受け取ります。
Martin Enderのおかげで-1バイト
-histocratのおかげで2バイト

sub /([aeiou]+([^aeiou]*)){,2} \g<2>/,""

オンラインでお試しください!

GNU sed、39 37 + 1 = 38バイト

-Eフラグ用に+1バイト。STDINでスペースで区切られた入力を受け取ります。
マーティン・エンダーのおかげで-1バイト

s/([aeiou]+[^aeiou]*){,2} [^aeiou]*//

オンラインでお試しください!

これは文字通り同じソリューションであるため、これを別個の回答として投稿しないでください。


素敵な正規表現!JSの回答でその一部を使用する場合は注意してください。
ETHproductions

もちろん、夢中になります。
ヨルダン

3
[^aeiou]/([aeiou]+([^aeiou]*)){,2} \g<2>/
サブ


1
@Anko この回答の「特別な呼び出し」を参照してください。TL; DR:デフォルトの呼び出しに加えて、バイトのみをカウントします。Rubyのデフォルトの呼び出しはruby -e "..."です。このためruby -pe "..."、であるため、1バイトしか追加しません。
ヨルダン

12

MATL、31 30バイト

t13Y2XJmFwhdl=fql_):)itJmYsg)h

オンラインで試す

説明

t       % Implicitly grab the input and duplicate it
13Y2    % Push the string literal 'aeiouAEIOU'
XJ      % Store this in clipboard J for later use
m       % Check which characters from the input are vowels (true for vowel)
Fwh     % Prepend FALSE to this logical array
dl=     % Compute the difference and find where we went from not-vowel to vowel
f       % Find the indices of these transitions
q       % Subtract 1 to get the location of the last consonant in each transition
l_)     % Get the next-to-last one of these
:)      % Grab the first string up to this location

% Now for the second component!

it      % Explicitly grab the input and duplicate
J       % Retrieve the string literal 'aeiouAEIOU' from clipboard J
m       % Find where the vowels are (true for vowel)
Ys      % Compute the cumulative sum along the array. The result will be 0
        % for all characters before the first vowel and non-zero after
g)      % Convert to logical and use this as an index so any characters
        % after the first value are retrieved

% Now to combine them

h       % Horizontally concatenate the first and second pieces together
        % Implicitly display the result

1
私はいつも幸せなコードに賛成票を投じています
アンドラスディーク

12

JavaScript(ES6)、81 73 72バイト

@Jordanのおかげで8バイト、@ DavidConradのおかげで1バイト節約

a=>b=>a.match(/.*?(?=(?:[aeiou]+[^aeiou]*){1,2}$)/)+b.match(/[aeiou].*/)

たとえ.match戻り配列array+array(すなわち、連結配列の内容の文字列を返し[0]+[1]戻ります"01")。

テストスニペット

Jordanの優れたRubyソリューションは、JSで53バイトになります。

x=>x.replace(/([aeiou]+[^aeiou]*){1,2} [^aeiou]*/,"")

たぶん、マッチビットを捨てて置き換えを使用できますか?
コナーオブライエン

@ ConorO'Brienそれはちょっとジョーダンの答えを盗むような気がする:/
ETHproductions

あなたは文字通り私の心を読んでいます。そしてええ、それは本当です
コナーオブライエン

1
1バイト(a,b)=>a=>b=>節約するためにカリー化。
デビッドコンラッド

7

ゼリー23 22 バイト

eۯcT
ǵḟ‘-ị’
Ç⁸ḣ⁹ÑḢ⁹ṫ

TryItOnline

どうやって?

eۯcT    - Link 1, vowel indexes: s   e.g. "colouring"
  Øc     - yield vowels, "AEIOUaeiou"
e€       - in for each                     [0,1,0,1,1,0,1,0,0]
    T    - truthy indexes (1-based)        [2,4,5,7]

ǵḟ‘-ị’  - Link 2, n-1th or only vowel group index start - 1: s
 µ       - monadic chain separation
Ç        - call last link (1) as a monad   [2,4,5,7]
   ‘     - increment                       [3,5,6,8]
  ḟ      - filter out                      [2,4,7]
    -    - -1
     ị   - index value                     [4]
               (Jelly is 1-based and has modular indexing,
                so the last but one item is at index -1,
                and when there is only 1 item in the list it is also at index -1)
      ’  - decrement                       [3]

Ç⁸ḣ⁹ÑḢ⁹ṫ - Main link: a, b                      e.g. "colouring", "pencils"
Ç        - call last link (2) as a monad with a      [3]
 ⁸       - link's left argument, a
  ḣ      - head a[:y]                                "col"
   ⁹  ⁹  - link's right argument, b
    Ñ    - call next link (1) as a monad                          [2,5]
     Ḣ   - pop head                                               [2]
       ṫ - tail b[y-1:]                                           "encils"
         - implicit print                            "colencils"

美しく説明!
Pureferret

5

PowerShell v2 +、76バイト

param($n,$m)($n-replace'([aeiou]+[^aeiou]*){1,2}$')+($m-replace'^[^aeiou]*')

どうやらこれは人気の正規表現です... ;-)

-replace演算子を使用して適切なピースを取り出し、結果を文字列で連結します。$最初にa を追加して文字列の最後^を確実に引き出し、2番目にa を追加して文字列の前部を確実に引き出します。


4

網膜、35バイト

([aeiou]+[^aeiou]*){1,2} [^aeiou]*

オンラインでお試しください!(最初の行は、改行で区切られたテストスイートを有効にします。)

最初の行で正規表現のすべての一致を削除するだけです。


1
母音と非母音のクラスを追加する予定はありますか?;-)
ETHproductions

@ETHproductions独自の正規表現フレーバーを実装することに悩むことができる(または、少なくとも.NET正規表現に変換できるようにトークン化する)ことができる場合は、必ず!:P
マーティンエンダー

Rubyは、パターンの後方参照を実行できます(異なる文字シーケンスに一致する同じパターン)。これらはここで役立ちます。たとえば、一致する括弧は/^((\(\g<1>\))*)$/Rubyで一致します。
ジョン・ドヴォルザーク

1
@JanDvorak残念ですが、Retinaは.NETで書かれていますよね?;)フレーバーを切り替えることができるようにgithub.com/ltrzesniewski/pcre-netにバンドルすることを検討しましたが、まだそれを回避できず、他のいくつかの機能は.NET固有のマッチ動作にますます依存しています。
マーティンエンダー

1
.net固有の動作をドロップし、Rubyですべてを書き換えるときですか?とにかくRubyの方が良い:
ジョンドヴォルザーク

4

シナモンガム、23バイト

0000000: 64d3 884e 4ccd cc2f 8dd5 8e8e 8330 b434  d..NL../.....0.4
0000010: b108 d92b c0d9 00                        ...+...

オンラインでお試しください。

説明

これによりが解凍されd([aeiou]+[^aeiou]*)([aeiou]+[^aeiou]*)? [^aeiou]*dその正規表現に一致するものがすべて削除されます。(ジョーダンのゴルファーは、d([aeiou]+[^aeiou]*){,2} [^aeiou]*圧縮する要素が繰り返されないため、24バイトに圧縮されることに注意してください。)


d[aeiou]+[^aeiou]*[aeiou]*[^aeiou]* [^aeiou]*どの短くなりますか?
-ETHproductions

私はそれが同じバイト数:(だった、という試みた@ETHproductions
spaghetto

3

PHP、95バイト

$t="aeiou]";echo($p=preg_filter)("#([$t+[^$t*){1,2}$#","",$argv[1]).$p("#^[^$t*#","",$argv[2]);

preg_filter 110バイトの代わりにpreg_matchを使用

$t="aeiou]";($p=preg_match)("#(.*?)([$t+[^$t*){1,2}$#",$argv[1],$m);$p("#[$t.*#",$argv[2],$n);echo$m[1].$n[0];

1
+代わりに使用できます{1,2}
タイタス

より重要な@Titusが1の場合のバグを排除することだったと今私はゴルフそれを下にしてみてください
イェルクHülsermann

を使用$v=aeiou;してさらに3つ保存します。
タイタス

@Titus私は同じ考えを持っていましたが、少しバリエーションがありました。ありがとう
ヨルグヒュルサーマン

3

Lua、66バイト

$ cat merge.lua
print(((...):gsub(("[]+[^]*[]*[^]*+[^]*"):gsub("]","aeiou]"),"")))
$ lua merge.lua brad+angelina
brangelina
$ lua merge.lua programming+puzzle
progruzzle

2

Perl 5、39バイト

38、プラス1の-pe代わりに-e

s/([aeiou]+[^aeiou]*){1,2} [^aeiou]*//

ハットチップ。


内にリンクされたsedの回答と同じですが、Perlでも同様に使用できます。
msh210


2

Lithp、65バイト

#X::((replace X (regex "([aeiou]+[^aeiou]*){1,2} [^aeiou]*") ""))

これは基本的に、上記のJavaScript回答の移植版で、私のLisp風の関数型プログラミング言語です。

使用例:

(
    % Note, you can define this as a function, or assign it to a variable
    % and use the call function instead.
    (def f #X::((replace X (regex "([aeiou]+[^aeiou]*){1,2} [^aeiou]*") "")))
    (print (f "programming puzzle"))
)

オンライン通訳はまだありません。すぐに提供します。難しいことではありません。私の言語はJavaScriptで書かれています。

代わりに、このパズルソリューションは私の言語の実例として実装されています。次のコマンドで実行できます。

node run.js l_src/progruzzle-colf.lithp

2

ハスケル、111の 108バイト

v x=elem x"aeiou"
d=dropWhile
e=d v
k=d$not.v
r=reverse
f a|c<-e.k.e.k$a,""/=c=c|1<3=e.k$a
a!b=(r.f.r)a++k b

この正規表現以外のソリューションは、予想よりも長くなりました。とにかくイデオネ。



弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.