極大値の抽出


19

正の整数の配列を指定すると、隣接する要素以上のすべての要素の配列を出力します。ほとんどの要素には、2つの隣接する要素があります。最初と最後の要素は、隣接する要素が1つしかないため、特殊なケースです。

配列には少なくとも2つの要素が含まれていると想定できます。

テストケース:

Input               | Output
[4,2,6,12,4,5,4,3]  | [4,12,5]
[1,2]               | [2]
[1,2,3,2,1]         | [3]
[3,2,1,2,3]         | [3,3]
[4,4]               | [4,4]
[2,4,4,4,1]         | [4,4,4]
[2,3,3,4]           | [3,4]
[4,3,3,4]           | [4,4]

これは、最短のコードが勝ちます!


1
@PeterTaylor私が意図しているのは、「出力に含まれる最初または最後の要素について...」
xnor

@PeterTaylor xnorは正しいです。
パベル



[4,3,3,4]テストケースとして提案できますか。私のソリューションは、残念ながらそれをうまく処理しませんでした。
JAD

回答:


5

ゼリー 13 12  11 バイト

0;;0»3\f"⁸Ẏ

正の整数のリストを取り、すべての隣人以上のリストのみを含むフィルター済みリストを返すモナドリンク。

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


前の12バイト

0;INżI>0ḄNMị

前の13バイト

0;;0ṡ3M€ċ€2Tị

どうやって?

0;;0»3\f"⁸Ẏ - Link: list of positive integers, A
0;          - a zero concatenated with A
  ;0        - concatenate a zero
     3\     - 3-wise reduce with:
    »       -   maximum (yields a list of the maximums in each overlapping window of 3)
         ⁸  - chain's left argument, A
        "   - zip with:
       f    -   filter keep (i.e. keep the maximal if it is [in] the [length 1 list 
            -                     of the] respective original element)
          Ẏ - flatten by one level

まあ私は3ワイズリダクションを使用する方法があるかもしれないと思うが、私はそれを解決していません。
ジョナサンアラン

私は正しかった-最大ダイアドで3ワイズリデュース»-でも10はどうだろうか?
ジョナサンアラン



6

Haskell、50 49 42バイト

f l=[j|i:j:k:_<-scanr(:)[0]$0:l,k<=j,i<=j]

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

scanr(:)[0] の尾のリストを作成します (0:l)、最終的で、それぞれ0について例えば、l = [4,3,3,4][[0,4,3,3,4,0],[4,3,3,4,0],[3,3,4,0],[3,4,0],[4,0],[0]]パターンマッチagainsがあるi:j:k:_と命名され、少なくとも3つの要素を持つすべてのリストを抽出するためにijなどをkj> = iおよびの場合は保持しjます。

編集:ØrjanJohansenは7バイトを節約しました。ありがとう!


2
i:j:k:_<-scanr(:)[0]$0:l短いです。(「標準」tails=scanr(:)[]トリックを少し調整します。)
ØrjanJohansen

@ØrjanJohansen:ああ、私は自分の前でそのトリックを使用しましたが、どういうわけかここでそれを逃しました。どうもありがとう!
nimi

4

Dyalog APL、31 30 28 22 21バイト

{⍵/⍨(⌈/=2⌷⊢)¨3,/∊0⍵0}

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

説明(私は物事を説明するのが苦手です):

0⍵0       - [0,input,0]   (it looks like a face!)
∊         - flatten
3,/       - split into overlapping sections of length 3.
(⌈/=2⌷⊢)¨ - Whether the middle element is the maximum (applied to every section)
⍵/⍨       - index


3

JavaScript(ES6)、40バイト

a=>a.filter((e,i)=>!(e<a[i-1]|e<a[i+1]))



2

05AB1E15  14  13バイト

ü‹0¸«sĆÁü›+_Ï

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

説明

ü‹             # pairwise comparison for less than
  0¸«          # append 0
     s         # swap input to top of stack
      Ć        # enclose, append the head of the list
       Á       # rotate right
        ü›     # pairwise comparison for greater than
          +    # add the two boolean lists
           _   # logical negate
            Ï  # keep only elements of input that are true in the resulting list

以前の15バイトソリューション

¬s¤)˜Œ3ùεZQ1è}Ï

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

説明

¬                # get head of input
 s¤              # get tail of input
   )˜            # wrap stack in flattened list
                 # produces the input list with the first and last element duplicated
     Œ3ù         # push sublists of length 3
        ε        # apply transformation on each triple
         ZQ      # ... check each element for equality to the max
          1è     # ... get the middle element
            }    # end transform
             Ï   # keep only elements of input that are true in the resulting list

2

R、44バイト

pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])

関数に評価されます:

function (x) 
x[(x >= c(0, x) & x >= x[-1])[1:sum(x | 1)]]

と比較xしてc(0,x)x1つ右にシフトします。また、と比較xしてx[-1]、1つの位置が左にシフトします。これらの両方はTRUE、そこに最大値がある場合です。&これらのブール値のANDを取る。Rのベクトルが同じ長さでない場合のラッピングの性質のため、次の長さで結果を切り捨てる必要があります。xがあります。sum(x|1)。次に、ブールベクトルをプラグインし、の真のインデックスのみを取得してx返します。

これらの論理演算は不等長のベクトルで実行されるため、Rが文句を言うことに注意してください。たくさん。ただし、警告の中で正しい出力が表示されます。

> pryr::f(x[(x>=c(0,x)&x>=x[-1])[1:sum(x|1)]])(c(4,2,6,12,4,5,4,3))
[1]  4 12  5
Warning messages:
1: In x >= c(0, x) :
  longer object length is not a multiple of shorter object length
2: In x >= x[-1] :
  longer object length is not a multiple of shorter object length
3: In x >= c(0, x) & x >= x[-1] :
  longer object length is not a multiple of shorter object length






1

q、39バイト

{x where x = -1 _ next 3 mmax x,last x}

この言語について聞いたことがありません。試してみたりダウンロードしたりできる場所を知っていますか?
パベル

もちろん、kx.com、docs:code.kx.com
skeevey

1

スタックス、10 バイト

úâH◄(☼bM•Å

実行してデバッグする

標準出力に改行で区切られた値として出力を生成します。

開梱されていない、コメントされていない、これはこのように見えます。

f       filter each value in input using the rest of the program; implicitly printing kept values
  x0|S  input pre- and post-pended with zero
  3B    split into batches of 3
  i@    get the i-th batch, where i is the iteration index
  |M=   is the current value equal to the max from the batch?

これを実行する

更新しました: 9バイトのソリューションが見つかりました。説明は後で更新します。

スタックス、9 バイト

▀▓ûa¥╓╧↨⌐

実行してデバッグする


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