建物を隠す


15

Skyscrapers Challengeの短いバージョン

仕事

建物の高さの配列と正の整数を指定すると、建物がk正確kに見えるように、高さのすべての順列(重複なし)を見つけます。

建物はすべて、背の低いまたは等しい高さの建物をすべて非表示にします。

入力および出力の形式はすべて有効です。

入力配列が空になることはありません。

正確に同じ数の建物を見ることができない場合は、答えではなくエラーではないものをすべて出力します。

例:

(出力の長さは非常に長い出力に対して表示されますが、出力はすべての可能な順列でなければなりません)

input:[1,2,3,4,5],2
output: 50

input:[5,5,5,5,5,5,5,5],2
output: []

input:[1,2,2],2
output:[(1,2,2)]
Seeing from the left, exactly 2 buildings are visible.

input:[1,7,4],2
output:[(4, 7, 1), (1, 7, 4), (4, 1, 7)]

input:[1,2,3,4,5,6,7,8,9],4
output:67284

input:[34,55,11,22],1
output:[(55, 34, 11, 22), (55, 22, 34, 11), (55, 34, 22, 11), (55, 11, 34, 22), (55, 22, 11, 34), (55, 11, 22, 34)]

input:[3,4,1,2,3],2
output:31

これはコードゴルフなので、最短のコードが勝ちます

オプション:可能であれば、のようなものを追加できますかif length is greater than 20: print length else print answer。コードではなく、フッターに。


出力はすべて適格な順列、またはその数である必要がありますか?
ルイスメンドー

すべての修飾順列@LuisMendo
Vedant Kandoi

推奨されるテストケース:[1,2,3,4,5],5 -> [(1,2,3,4,5)]。現在のテストケースでは、回答がすべての建物の表示をサポートできることを確認していません(ただし、実際に問題があるかどうかはわかりません)。
カミルドラカリ

回答:


6

05AB1E10 9 バイト

œÙʒη€àÙgQ

オンラインそれを試してみたり(ほとんど)すべてのテストケースを検証(テストケース[1,2,3,4,5,6,7,8,9],4がタイムアウトを)。
TIOのフッターは、OPが下部で要求したことを実行します。

オプション:可能であれば、のようなものを追加できますかif length is greater than 20: print length else print answer。コードではなく、フッターに。

説明:

œ            # Permutations of the (implicit) input-list
             #  i.e. [1,2,2] → [[1,2,2],[1,2,2],[2,1,2],[2,2,1],[2,1,2],[2,2,1]]
 Ù           # Only leave the unique permutations
             #  i.e. [[1,2,2],[1,2,2],[2,1,2],[2,2,1],[2,1,2],[2,2,1]]
             #   → [[1,2,2],[2,1,2],[2,2,1]]
  ʒ          # Filter it by:
   η         #  Push the prefixes of the current permutation
             #   i.e. [1,2,2] → [[1],[1,2],[1,2,2]]
    ۈ       #  Calculate the maximum of each permutation
             #   i.e. [[1],[1,2],[1,2,2]] → [1,2,2]
      Ù      #  Only leave the unique maximums
             #   i.e. [1,2,2] → [1,2]
       g     #  And take the length
             #   i.e. [1,2] → 2
        Q    #  And only leave those equal to the second (implicit) input
             #   i.e. 2 and 2 → 1 (truthy)

1
印象的な、ここのすべてのバイトは関数ツリーの一部です!
リルトシアスト

1
@lirtosiastええ、05AB1Eにはいくつかの非常に短いビルトインがありますが、このチャレンジでは完璧でした。:)それは実際に私が見るあなたのPythの答えに非常に似ています。面白いのは、フッターif length is greater than 20: print length; else print answer;がプログラム自体と比較して等しい長さのa̶̶b̶y̶t̶e̶̶l̶o̶n̶g̶e̶r̶であることです。xD
ケビンクルーイッセン


5

ゼリー12 10バイト

Œ!Q»\QL=ʋƇ

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

@Erik the Outgolferによる-2バイト

これは、建物の高さをkその順序で取得する二項関数です。

Œ!                All permutations of first input
Œ!Q               Unique permutations of first input
   »\              Running maximum
     Q             Unique values
      L            Length of this array
       =           Equals k
        ʋ        Create a monad from these 4 links
   »\QL=ʋ        "Are exactly k buildings visible in arrangement x?"
         Ƈ     Filter if f(x)
Œ!Q»\QL=ʋƇ     All distinct perms of first input with k visible buildings.

1
新しいあられʋƇ実際には:P よりかなり古いです)
エリックアウトゴルファー

4

Pyth、18 16バイト

fqvzl{meSd._T{.p

ここで試してみてください

Pythインタープリターのオンラインバージョンは、最大のテストケースでメモリエラーをスローすることに注意してください。

f                       Filter lambda T:
  q                       Are second input and # visible buildings equal?
    v z                     The second input value
    l {                     The number of unique elements in
        m                   the maximums
          e S d             ...
          ._ T              of prefixes of T
    { .p                  over unique permutations of (implicit first input)

お帰りなさい!:-)
ルイスメンドー

2

Perl 6の81の 63バイト

nwellnhofのおかげで-18バイト!

{;*.permutations.unique(:with(*eqv*)).grep:{$_==set [\max] @_}}

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

カリー化された入力を受け取る匿名コードブロックf(n)(list)。それ.unique(:with(*eqv*))は迷惑なほど長いですが:(

説明:

{;                                                            }  # Anonymous code block
  *.permutations.unique(:with(*eqv*))  # From all distinct permutations
                                     .grep:{                 }  # Filter where
                                                set [\max] @_   # Visible buildings
                                            $_==      # Equals num

1
FWIW、私は楽堂の問題を提出したので、;最終的にはその迷惑なものを取り除くかもしれません;)
nwellnhof

2

ジャプト、11バイト

á f_åÔâ Ê¥V

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

長い出力の場合、追加 } l最後すると代わりに長さが出力されます。オンラインインタプリタ[1,2,3,4,5,6,7,8,9],4は、長さまたはリストの出力に関係なく、テストケースに対してタイムアウトします。

説明:

á              :Get all permutations
  f_           :Keep only ones where:
    åÔ         : Get the cumulative maximums (i.e. the visible buildings)
      â Ê      : Count the number of unique items
         ¥V    : True if it's the requested number

1

JavaScript(ES6)、108 107バイト

入力をとして受け取ります(k)(array)。結果をで出力しalert()ます。

k=>P=(a,p=[],n=k,h=0)=>a.map((v,i)=>P(a.filter(_=>i--),[...p,v],n-(v>h),v>h?v:h))+a||n||P[p]||alert(P[p]=p)

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

コメント済み

k =>                        // k = target number of visible buildings
  P = (                     // P = recursive function taking:
    a,                      //   a[] = list of building heights
    p = [],                 //   p[] = current permutation
    n = k,                  //   n = counter initialized to k
    h = 0                   //   h = height of the highest building so far
  ) =>                      //
    a.map((v, i) =>         // for each value v at position i in a[]:
      P(                    //   do a recursive call:
        a.filter(_ => i--), //     using a copy of a[] without the i-th element
        [...p, v],          //     append v to p[]
        n - (v > h),        //     decrement n if v is greater than h
        v > h ? v : h       //     update h to max(h, v)
      )                     //   end of recursive call
    )                       // end of map()
    + a ||                  // unless a[] was not empty,
    n ||                    // or n is not equal to 0,
    P[p] ||                 // or p[] was already printed,
    alert(P[p] = p)         // print p[] and store it in P


0

J、43 38バイト

KevinのO5AB13回答から最適化を組み込んだ後の-5バイト

(]#~[=([:#@~.>./\)"1@])[:~.i.@!@#@]A.]

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

食べない

(] #~ [ = ([: #@~. >./\)"1@]) ([: ~. i.@!@#@] A. ])

説明

可能なすべてのパーマをリストしているだけでi.@!@#@] A. ]、そのuniqアイテムを~.、表示されている建物の数でそれらをフィルタリングします。これは左の入力と等しくなければなりません。

重要なロジックは、目に見える建物の数を計算する括弧付きの動詞にあります。

([: #@~. >./\)

ここでは、最大スキャンを使用して、>./\これまでに見られた最も高い建物の集計を保持します。次に、最大スキャンの一意の要素を取得します。これは、目に見える建物の数です。

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