増加、減少、なし、またはすべて?


9

数字12文字列を含む空でないベクトル/リストの2つの入力を受け取ります(いいえ、0/1代わりに受け取ることはできません)。文字列は次のいずれかになります(小文字で表記されているとおり)。

increasing
decreasing
ones
twos
all
none

文字列が____の場合、インデックス___を返す必要があります。

  • increasing...リストがから1に変わる場所2(の2直後に続くすべて1
  • decreasing...リストがから2に変わる場所1(の1直後に続くすべて2
  • ones ...すべての桁の 1
  • twos ...すべての桁の 2
  • all ...すべての数字
  • none...数字のどれも。0リストが1インデックスの場合は問題ありません。リストのインデックスが0の場合、負の数でも問題ありません。空のリストまたは文字列を出力することもできます。

テストケース:

これらは1インデックスです。1インデックスにするか0インデックスにするかを選択できます。テストケースの異なる文字列には同じベクトルが使用されます。

--------------------------------
Vector:
1 1 2 2 2 1 2 2 1 1 2

String       - Output
increasing   - 3, 7, 11
decreasing   - 6, 9
ones         - 1, 2, 6, 9, 10 
twos         - 3, 4, 5, 7, 8, 11
all          - 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
none         - 0 / []

------------------------------------
Vector:
1

String:
ones         - 1
all          - 1
decreasing / increasing / twos / none  - 0 / []

得点

これは、バイト数が最も少ない答えが優先されます。

説明が奨励されます!


@RobertoGrahamはい。
Stewie Griffin

@KevinCruijssenあなたは良い推測者です:)
Stewie Griffin

これまでのところ、例に示すようなリストを出力する回答はありません。(つまり、末尾の区切り文字なしで "、"で結合されます)。チャレンジテキストにはリストの柔軟性が記載されていないため、このようなチャレンジに対して通常受け入れられるものは何ですか?
Tahg

通常は非常に柔軟です。数値のリストである限り、問題ありません。
Stewie Griffin

回答:


7

JavaScript(Firefox 30-57)、74 73バイト

(a,[s],i=0,p)=>[for(e of a)if({i:e>p,d:e<p,o:e<2,t:e>1,a:1}[p=e,i++,s])i]

配列内包は、組み合わせのきちんとした方法ですmapfilter一度インチ 編集:@ edc65のおかげで1バイトが節約されました。


3

パイソン2136の 131 119 108 97バイト

  • 5バイトを保存。lambda関数を使用します。
  • TFeldのおかげで12バイトが節約さました。ゴルフの2つの条件。
  • Xcoder氏のお陰で11バイト節約。のenumerate()代わりに使用range(len())
  • 辞書の代わりにリストを使用し、0-indexing(TFeldの回答のように)を使用してにゴルフ"adinot".find(m[0])することにより、11バイトを節約しましたord(m[0])/3-32
lambda l,m:[j for j,k in enumerate(l)if[1,j*k<j*l[~-j],0,j*k>j*l[~-j],0,k<2,k>1][ord(m[0])/3-32]]

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


入力は常にあるので1または2、あなたが変更することができます(l[j]>1)*(l[~-j]<2)(l[j]>l[~-j]) するために119バイト
TFeld

また、0インデックスに切り替えることでバイト節約できます
TFeld

@TFeldありがとうございます。私は1-indexedに固執すると思います。
Jonathan Frech 2017

108バイト、使用enumerate()
Mr. Xcoder



2

MATL32 31 30 29バイト

dQ~fQGqfOOGofGd1=fQGfO[]Xhjs)

出力は1ベースまたは空です。

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

説明

コードは、配列入力の6つの可能な出力を計算し、文字列入力に応じて適切な出力を選択します。

出力を選択するために、文字列入力のすべての文字のASCIIコードポイントが追加されます。結果モジュロ9が得られる615270それぞれのために'increasing''decreasing''ones''twos''all''none'。結果の数値はすべて異なるため、これを選択基準として使用できます。

合計に対して9を法とする演算を実際に実行する代わりに、可能な入力のリストは9エントリ(一部はダミー)に拡張されるため、そのリストへのインデックス付けは9を法として自動的に行われます。

d     % Implicit input: numeric vector. Push vector of consecutive differences.
      % Contains -1, 0 or 1
Q~    % For each entry: add 1, negate. This turns -1 into 1, other values into 0
f     % Push indices of nonzeros
Q     % Add 1 to each entry (compensates the fact that computing consecutive
      % differences removes one entry). This the output for 'decreasing'
Gq    % Push input again. Subtract 1 from the code points
f     % Push indices of nonzeros. This is the output for 'twos'
OO    % Push two zeros. These are used as placeholders
Go    % Push input and compute parity of each entry
f     % Push indices of nonzeros. This is the output for 'ones'
Gd    % Push input and compute consecutive differences
1=    % Test each entry for equality with 1
f     % Push indices of nonzeros 
Q     % Add 1. This is the output for 'increasing'
Gf    % Push indices for all input (nonzero) entries. This is the output for 'all'
O     % Push zeros. Used as placeholder
[]    % Push empty array. This is the output for 'none'
Xh    % Concatenate stack into a cell array
j     % Input a string
s     % Sum of code points
)     % Use as an index into the cell aray. Implicitly display


1

ゼリー、27バイト

>2\0;
NÇ
Ị
=2

ḟ
⁹Ḣ“hɠ»iµĿT

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

-3 Jonathan Allanに感謝します。


辞書の単語「diota」を使用して3バイトを節約します -リンク0で正しくなることに注意してください。ただし、再度並べ替えて「antidote」などの単語を使用すると、テストハーネスを再び機能させることができます。
ジョナサンアラン

@JonathanAllanリンク0は一番下のリンクだと思いましたが、どうやらÇ奇妙ですが、ありがとう!(また、私は新しい単語を学んだ:p)
Erik the Outgolfer '30 / 09/30

1

Husk、27バイト

`fN!+mmëI=2ε¬moΘẊe><€¨Ÿȧö¨←

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

-9 H.PWizに感謝します

私はこの答えをとても誇りに思っています。


Golfed使用することにより、大部分ΘẊ>ΘẊ<し、`fN
H.PWiz

@ H.PWiz私はそれらを正直にどのように見ましたか
Erik the Outgolfer

-1バイトインデックスが付けられたリスト0が最後の要素です。
H.PWiz 2017

@ H.PWiz Ooh圧縮された文字列が¨₆Żσa¨代わりになると思ったので、その機能を使用しませんでした。おかげで。そして今、私はそれがジェリーを結びつけると言うことができます。
Erik the Outgolfer 2017

1

JAVA(OpenJDKの8) 266 217 213 205 172の 171 155 131バイト

s->a->{int i=0,l=0,c=s.charAt(0)-97;for(int e:a){if(++i>1&(c==8&e>l|c==3&e<l)|c==14&(l=e)<2|c>18&l>1|c<1)System.out.print(i+",");}}

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


yとして定義すると、やのcharようなゴルフの同等性テストを実行できます。y.equals("a")y=='a'y==97y<98
Jonathan Frech 2017

@JonathanFrechはちょうどそれを変更していました:)
Roberto Graham

TIOは少なくとも私が期待する出力ではありません。例としてのみ示されていますが、リストには要素間にスペースが必要で、末尾にコンマは必要ありません。
Tahg

以来19IS cの最高値c==19に等しいですc>18
Jonathan Frech 2017

2
131バイト:s->a->{int i=0,l=0,c=s.charAt(0)-97;for(int e:a){if(++i>1&(c==8&e>l|c==3&e<l)|c==14&(l=e)<2|c>18&l>1|c<1)System.out.print(i+",");}}
Nevay

1

Jq 1.5、131バイト

文字列のマッチングは私の配列バージョンよりも短いため、xcaliのアプローチに基づいています。

def D(s):[.[1]|gsub(" ";"")|match(s;"g").offset+(s|length)];./"
"|{i:D("12"),d:D("21"),o:D("1"),t:D("12"),a:D("."),n:[]}[.[0][0:1]]

jqが-Rsオプション付きで呼び出され、入力が2行で表示されると仮定します。

decreasing
1 1 2 2 2 1 2 2 1 1 2

拡張:

def D(s): [
      .[1]                              # find where s appears
    | gsub(" ";"")                      # in the input and add
    | match(s;"g").offset + (s|length)  # length to get ending index
  ]
;

  ./"\n"                                # split on newline
| {i:D("12"),                           # increasing
   d:D("21"),                           # decreasing
   o:D("1"),                            # ones
   t:D("2"),                            # twos
   a:D("."),                            # all
   n:[]                                 # none
  }[.[0][0:1]]

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



1

J、73バイト

g=.[:I.[=0,2-/\]
(_1 g])`(1 g])`(1=])`(2=])`(i.@#@])`_1:@.('idotan'i.{.@[)

これがどのように大幅に圧縮されるかを知りたいと思います-私はそれができると信じています(これらすべての議題の括弧だけで10文字です!)

  • g-増加と減少のためのヘルパー動詞。これ\は、サイズ2のinfix runの値を比較するだけです。
  • 残りは単に「コマンド」から最初の文字を取得し、アジェンダを使用して対応するケースを実行します @.

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


やる1=]2=]仕事をしませんか?また、g左の引数として数値を取り、右の引数としてリストを取り、リストに2-/\ 適用されたインデックスを左の引数と等しくした場合はどうなるでしょうか。そうすれば、副詞を使用する代わりに、_1それ1を渡したり、減少したり増加したりすることができます。
cole 2017

@cole良いコメント。私は変更を加えましたが、結果ははるかにきれいですが、73は依然としてバイト数が多いようです。つまり、JはJSをここで結びつけています。
ジョナ

0

Java 8、233 229 216バイト

l->s->{int i=s.charAt(0)-97,k=0,j=1;for(s=(l+"").replaceAll("[^12]","");s.length()*j>0;System.out.print(j++<0?"":(k+=j)+","),s=s.substring(j))j=i<1?0:s.indexOf(i<4?"21":i<9?"12":i<14?" ":i<15?"1":"2")+(i>2&i<9?1:0);}

このStringのアプローチは、予想よりも長くなってしまいました。しかし、他のJava 8の回答にひどく圧倒されと思っていても、とにかく投稿することにしました。
このアプローチを使用しても、間違いなくゴルフを楽しむことができます。「なし」と「増加/減少」は、主にいくつかのバイトを消費するいくつかの回避策を引き起こしました。

結果は1インデックスです。

説明:

ここで試してください。

l->s->{                          // Method with List and String parameters
  int i=s.charAt(0)-97,          //  First character of the String - 97
                                 //   (i=8; d=3; o=14; t=19; a=0; n=13)
      k=0,                       //  Total counter
      j=1;                       //  Index integer
  for(s=(l+"")                   //  toString of the List,
         .replaceAll("[^12]","");//   and leave only the 1s and 2s 
      s.length()*j>0             //  Loop as long as `j` and the size of the String
                                 //  are both larger than 0
      ;                          //   After every iteration:
      System.out.print(          //    Print:
       j++<0?                    //     If `j` is -1:
        ""                       //      Print nothing
       :                         //     Else:
        (k+=j)+",")              //      Print the current index
      ,s=s.substring(j))         //    And then remove the part of the String we've checked
    j=i<1?                       //   If "all":
                                 //    Change `j` to 0
      :                          //   Else:
       s.indexOf(                //    Replace `j` with the next index of:
        i<1?                     //     If "all":
         s.charAt(0)+""          //      The next character
        :i<4?                    //     Else-if "decreasing":
         "21"                    //      Literal "21"
        :i<9?                    //     Else-if "increasing":
         "12"                    //      Literal "12"
        :i<14?                   //     Else-if "none":
         " "                     //      Literal space (any char that isn't present)
        :i<15?                   //     Else-if "one":
         "1"                     //      Literal "1"
        :                        //     Else(-if "two"):
         "2")                    //      Literal "2"
       +(i>2&i<9?1:0);           //     +1 if it's "increasing"/"decreasing"
                                 //  End of loop (implicit / single-line body)
}                                // End of method

0

Perl 5、71 + 2(-nl)= 73バイト

$p=/l/?'.':/t/?2:/^o/?1:/d/?21:/i/?12:0;$_=<>;s/ //g;say pos while/$p/g

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

改訂されたロジックは、以下の説明と実質的に同じですが、パターンマッチが短縮されています。

以前は:

$p=/all/?'.':/^o/?1:/^t/?2:/^d/?21:/^i/?12:0;$_=<>;s/ //g;say pos while/$p/g

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

条件が一致しない場合は何も出力しません。

説明:

$p=          # set the pattern to seach based on the input string
  /all/?'.'  # any character
 :/^o/?1     # starts with 'o', find ones
 :/^t/?2     # starts with 't', find twos
 :/^d/?21    # starts with 'd', find decreasing
 :/^i/?12    # starts with 'i', find increasing
 :0;         # anything else: create pattern that won't match
$_=<>;s/ //g;# read the digits and remove spaces
say pos while/$p/g # output position(s) of all matches
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.