2D配列の反時計回りの内側スパイラルを出力します


15

この stackoverflow質問から

サイズがM×N 2D配列を指定すると、値を反時計回りに出力します。出力は、外側から内側に開始する必要があり、初期ポイントが常にあることを行っている(0,0

与えられた

[12345678910111213141516]

反時計回りのエッジ値は、ある1,5,9,13,14,15,16,12,8,4,3,2

ここで、内部値に対してプロセスを繰り返します。これは、次のような行列になります

[671011]

そして、内側値は、である6,10,11,7

最終的な結果は、あろう1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7


ルール


いくつかのテストケース

Input
[
  [1, 2, 3, 4, 5, 6, 7],
  [8, 9, 10,11,12,13,14],
  [15,16,17,18,19,20,21]
]
Output
1,8,15,16,17,18,19,20,21,14,7,6,5,4,3,2,9,10,11,12,13

--------------------------------------------------------

Input
[
    [1,2,3],
    [3,2,1],
    [4,5,6],
    [6,5,4],
    [7,8,9],
    [9,8,7]
]
Output
1,3,4,6,7,9,8,7,9,4,6,1,3,2,2,5,5,8

-----------------------------------------------------
Input
[
    [1]
]
Output
1
-----------------------------------
Input
[
    [1, 2],
    [2, 1]
]
Output
1,2,1,2
-----------------------------------------------------
Input
[
    [1,2,3,6,7],
    [2,4,3,2,1],
    [3,2,4,5,6],
    [6,5,6,5,4],
    [10,4,7,8,9],
    [12,4,9,8,7]
]
Output
1,2,3,6,10,12,4,9,8,7,9,4,6,1,7,6,3,2,4,2,5,4,7,8,5,5,2,3,4,6

では、時計回りに行くのですか、それとも反時計回りに行くのですか?
LegionMammal978

@ LegionMammal978反時計回り(反時計回りと呼ばれていましたが)
ルイスフェリペデジェススムニョス

7
反時計回りと反時計回りの両方が正しく、それぞれBrEngとAmEngでより一般的です。本当に混乱させたい場合は、widershinsも使用できます。
デジタル外傷

回答:


12

R、54バイト

@Giuseppeと@ J.Doeによって保存された数バイト。

f=function(m)if(ncol(m))c(m[,1],f(t(m[nrow(m):1,-1])))

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

最初の列を再帰的に取り除き、残りの行列を1行のみになるまで行の反転/転置(一番下の行を新しい最初の列にします)します。ゴルフをしていない「伝統的な」バージョン:

f <- function(m) {
 if(ncol(m) == 1) {
    m
  } else {
    c(m[,1], f(t(m[nrow(m):1,-1])))
  }
}

正の整数行列値を仮定することが許可さncol(m)sum(m)ているため、別のバイトを節約するためにゴルフをすることができることが指摘されました。しかし、すべての行列(文字列の行列も!)で機能するため、このようにします。


うわー!を使用するt()ことでdrop=TRUEデフォルト`[`if状態が悪化するのを防ぐ方法が大好きです!
ジュゼッペ

そして完全な開示により、約200バイトのソリューションで動作しませんでした。質問が報奨金の対象となると、この賞金を授与することになります。
ジュゼッペ

@Giuseppeが59バイトに戻りました!私は、元々の試みにあっt()is.nullテストを使用する必要がないことに驚きました。
ngm

mとにかく最後はnullにならないので、if-statementを54バイトに変更できます。テストケースで動作するようです。
J.Doe


7

Pyth、9バイト

shMM.utC_

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

どうやって?

shMM.utC_     Full program. Takes a 2D array (matrix) from STDIN.
    .u        Until a result that has occurred before is found, loop and collect...
        _     Reverse the matrix (reverse the order of its rows).
       C      Transpose.
      t       Remove first element.
 hMM          For each element in the resulting 3D array, get the heads of its elements.
s             Flatten.

カッコいい。Pythの初心者として、学ぶべきことがたくさんあることを知っています。
ElPedro18年

5

スタックス、7 バイト

ôQÖG·í<

実行してデバッグする

1行の行の配列を受け取り、改行で区切られた出力を生成します。

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

W       repeat the rest of the program until cancelled explicitly
  rM    rotate matrix *clock-wise* (yes, this is the opposite of the challenge)
  |c    assert matrix is truthy. (has rows) cancel otherwise.
  B     remove the top row of the matrix, and push separately to main stack
  rm    reverse the top row (this fixes the rotation direction), and print each value

これを実行する


4

Pyth、20バイト

J.TQWJ=+YhJ=J_.TtJ)Y

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

説明

J.TQWJ=+YhJ=J_.TtJ)Y
J.TQ                    Call the transposed input J.
    WJ            )     While J is not empty...
      =+YhJ             ... put the top row into Y (initially [])...
           =J   tJ      ... remove the top row...
             _.T        ... reverse and transpose (rotate clockwise).
                   Y    Output the result.

4

oK、12バイト

*+,/(1_+|:)\

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

これは、oKが移調の形をあまり気にしていないように見えるという事実を悪用します。kでは、これは13バイトになります*:',/(1_+|:)\

       +|:   /reverse, then transpose (rotate right)
     1_      /remove first line
    (     )\ /fixpoint of the above, keeping intermediate results (scan)
  ,/         /concatenate all the rows
*+           /get the first element of each row

3

クリーン、69バイト

import StdEnv,StdLib
? =transpose
@[h:t]=h++ @(reverse(?t))
@_=[]

@o?

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

引数でパターンマッチできるように、次の行/列をリストの先頭に移動します。

チャレンジの最初の例では、これは次のようになります。

@o? [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
@ h=:[1, 5, 9, 13] t=:[[2, 6, 10, 14], [3, 7, 11, 15], [4, 8, 12, 16]]
[1, 5, 9, 13] ++ @ h=:[14, 15, 16] t=:[[10, 11, 12], [6, 7, 8], [2, 3, 4]]
[1, 6, 9, 13, 14, 15, 16] ++ @ h=:[12, 8, 4] t=:[[11, 7, 3], [10, 6, 2]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4] ++ @ h=:[3, 2] t=:[[7, 6], [11, 10]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2] ++ @ h=:[6, 10] t=:[[7, 11]]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10] ++ @ h=:[11, 7] t=:[]
[1, 6, 9, 13, 14, 15, 16, 12, 8, 4, 3, 2, 6, 10, 11, 7] ++ @ []

3

ジュリア0.7、47バイト

f(m)=[m[:,1];sum(m)<1?[]:f(rotr90(m[:,2:end]))]

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

ジュリアには、行列を90度回転させる便利なビルトイン機能があり、転置-逆演算の必要性を排除しています。

コンパイラの警告からわかるように、三項条件式のすべてのコンポーネントはスペースで区切る必要があると主張しており、v。1.0ではこれが実際に実施されています。

奇妙なことに、この状況で再帰から抜け出すために見つけた最も短い方法は、try-catchブロックを使用することでした:

ジュリア1.0、50バイト

f(m)=[m[:,1];try f(rotr90(m[:,2:end]))catch;[]end]

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



2

APL(Dyalog)24 22バイト

{×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬}

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

どうやって?

{×≢⍵:⍵[;1],∇⍉⊖0 1↓⍵⋄⍬}
{                    } - a function
 ×≢⍵:                  - if non-empty:
     ⍵[;1]             - the left column
          ,∇⍉⊖0 1↓⍵    - repeat this function without the left column, rotated counter clockwise
                   ⋄⍬  - otherwise, return an empty vector

演算子の説明がいいでしょう。
Arc676

1
@ Arc676、追加されました!
ザカリー

2

05AB1E13 11 10 バイト

ΔRøćRˆ}¯˜þ

@Emignaのおかげで-2バイト。

オンラインそれを試してみたり、すべてのテストケースを確認してください

説明:

Δ         # Loop until the stack no longer changes:
 R        #  Reverse the order of the rows
          #   i.e. [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
          #    → [[13,14,15,16],[9,10,11,12],[5,6,7,8],[1,2,3,4]]
  ø       #  Zip, swapping rows and column
          #   → [[13,9,5,1],[14,10,6,2],[15,11,7,3],[16,12,8,4]]
   ć      #  Head extracted
          #   → [[14,10,6,2],[15,11,7,3],[16,12,8,4]] and [13,9,5,1]
    R     #  Reverse this row
          #   → [1,5,9,13]
     ˆ    #  Pop and push it to the global array
}         # After the loop:
 ¯        #  Push the global array
          #   i.e. [[1,5,9,13],[14,15,16],[12,8,4],[3,2],[6,10],[11],[7],"",""]
  ˜       #  Flatten it
          #   → [1,5,9,13,14,15,16,12,8,4,3,2,6,10,11,7,"",""]
   þ      #  Remove all empty string by only leaving all digits
          #   → ["1","5","9","13","14","15","16","12","8","4","3","2","6","10","11","7"]
          # (and output it implicitly)


1

、25バイト

≔⮌EA⮌ιθWθ«≔E§θ⁰⮌Eθ§μλθI⊟θ

オンラインでお試しください!リンクは、コードの詳細バージョンです。説明:

≔⮌EA⮌ιθ

入力を180°回転させます。これには2つの理由があります。a)最後の行を削除するのが最も簡単であり、b)ループの最後に行を削除するとループしやすくなります。(時計回りに反映して出力しようとしましたが、余分なバイトがかかりました。)

Wθ«

配列が空になるまで繰り返します。

≔E§θ⁰⮌Eθ§μλθ

アレイを90°回転させます。

I⊟θ

配列の最後の行を削除し、要素を文字列として別の行に出力します。



1

パワーシェル、266バイト

ええ.. PowerShellは、行列の処理に最適ではありません。しかし、アルゴリズムは基本的に上記と同じです。各行はカンマ区切りの文字列として表され、基本的に各レイヤーの回転と転置を行います。私はおそらくもっと多くを剃ることができますが、... すでに私のパジャマにいます...

Filter F{$a=$_-replace"],|]|\s",''-split'\['|?{$_-ne''};$b=@();while($a-ne $null){$N=($a[0]-split',').Count-1;$a=0..$N|%{$i=$_;($a|%{($_-split',')[$i]})-join','};if($d){[array]::Reverse($a)}if($N-gt0){$b+=$a[0];$a=$a[1..$N]}else{$b+=$a;$a=$null};$d=$true}$b-join','}

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

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