または、2D配列の列と行をシフトします


15

目的

任意のサイズの2D配列が与えられた場合、列と行を交互にシフトするプログラムまたは関数を作成します

a b c d e
f g h i j
k l m n o

内のすべての要素最初の列シフトダウン一列、第二列シフトアップ一列、第三のシフトダウン彼らはエッジを1行ように、ラッピングに到達したとき。

k g m i o
a l c n e
f b h d j  

最初の行のすべての要素はにシフトし、2番目に、3番目右に、など、端に達すると折り返されます。

o k g m i
l c n e a
j f b h d

最短の作業コードをベストアンサーとして選択するという伝統に従います。


配列は任意のサイズ、具体的には3x5にできますか?
ジョーキング

塗りつぶされた2D配列を探していました。言及していないことを申し訳ありません。編集を追加
カランShishoo

正直に言うと、不適切な書式設定により、質問は怠SOなSOユーザーからのトピック外の質問のように見えます。
user202729

(ところで、すぐに回答を受け入れないでください)
-user202729

5
@kshishoo将来の課題のためにあなたが使用できるサンドボックスを 重複をチェックし、および/またはメインのサイトに投稿する前にいくつかのフィードバックを収集するために
ロッド

回答:



7

MATL、13バイト

,!tZy:oEq2&YS

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

説明

,        % Do twice
  !      %   Transpose. Takes input implicitly the first time
  t      %   Duplicate
  Zy     %   Size. Gives a vector with numbers of rows and of columns
  :      %   Range from 1 to the first entry of the vector (number of rows)
  o      %   Parity: gives 0 or 1 for eacn entry
  Eq     %   Times 2, minus 1: transforms 0 into -1
  2      %   Push 2
  &YS    %   Circularly shift along the second dimension. This shifts the
         %   first row by 1 (that is, to the right), the second by -1 (to
         %   the left), etc.
         % End (implicit). Display (implicit)

6

J26、21の 19バイト

マイルのおかげで-5バイト

(|."_1~_1^#\)@|:^:2

説明:

^:2 -次を2回繰り返します。

@|: -転置して

#\ -プレフィックスの長さを見つけます(1、2、3 ...行)

_1^ --1を上記の累乗にし、-1 1 -1 1 1の交互のリストを作成します...

|."_1~ -上記のリストからのオフセットで入力配列の各行を回転します

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

元のバージョン:

(($_1 1"0)@#|."0 1])@|:^:2

使い方

^:2 -次を2回繰り返します。

|: -転置して

|."0 1] -入力配列の各行、リスト内のオフセットを回転させます。

@# -配列内の行数

($_1 1"0) -代替_1 1(3-> _1 1 _1)

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


1
_1 1..を使用して生成すること(|."_1~_1^2|#\)@|:^:2もできます
マイル

@milesありがとう、これは素晴らしいコードです!
ガレンイワノフ

実際には@miles私は2|一部を必要としません
ガレンイワノフ

1
はい、実際にはそうではありません。それはさらに2バイト節約されます。
マイル



2

APL + WIN、30バイト

2D配列の画面入力のプロンプト

((↑⍴m)⍴¯1 1)⌽((1↓⍴m)⍴¯1 1)⊖m←⎕

2

APL(Dyalog Unicode)、26バイト

{(¯1 1⍴⍨≢⍵)⌽(¯1 1⍴⍨≢⍉⍵)⊖⍵}

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

プレフィックスDfn。

どうやって?

{(¯1 1⍴⍨≢⍵)⌽(¯1 1⍴⍨≢⍉⍵)⊖⍵}⍝ Main function, prefix. Input matrix is ⍵.
                        ⊖⍵}⍝ Rotate the columns of  according to the left arg:
            (       ⍉⍵)     Transpose  (makes a 3x5 matrix become 5x3)
                           Tally (yields the number of rows of the matrix)
                           Swap arguments of the following fn/op
                           Shape
             ¯1 1           This vector. This yields a vector of ¯1 1 with size = number of columns of ⍵.
                           Rotate the rows of  according to the left arg:
{(¯1 1⍴⍨≢⍵)                 Does the same as the preceding expression, without transposing ⍵.


2

JavaScript(ES6)、94 91バイト

a=>(g=a=>a[0].map((_,i)=>(b=a.map(a=>a[i]),i%2?[...b.slice(1),b[0]]:[b.pop(),...b])))(g(a))

おそらく回転を行うためのゴルファーの方法があるでしょう...


2

Pyth、15バイト

L.e.>b^_1k.Tbyy

オンラインで試す

説明

L.e.>b^_1k.Tbyy
L           b      Define a function on a list...
          .T       ... which transposes it...
 .e.>b^_1k         ... and rotates each row alternating left and right.
             yyQ   Apply twice to the (implicit) input array.

2

q / kdb +、32バイト

解決:

{rotate'[#:[x+:]#-1 1](+)x}/[2;]

例:

q)3 5#.Q.a / reshape "a..o" into 3 row, 5 column grid
"abcde"
"fghij"
"klmno"
q){rotate'[#:[(+)x]#-1 1](+)x}/[2;]3 5#.Q.a
"okgmi"
"lcnea"
"jfbhd"

説明:

に回転を適用するためにグリッドを反転します。2回目の反復はもう一度反転し、回転が行に適用されます目のパスでにます。

回転は、回転-1 1 -1 1..する行/列の長さのリストに基づいています。

この読みやすいバージョンから健全な9バイトが削除されました

{rotate'[count[flip x]#-1 1;flip x]}/[2;] / ungolfed solution
{                                  }/[2;] / perform lambda 2 times
 rotate'[                  ;      ]       / perform rotate on each-both
                            flip x        / flip x<->y of grid
                      #-1 1               / take from list -1 1
         count[flip x]                    / the length of the flipped grid

2

JavaScript(ES6)、 116  76バイト

m=>(g=m=>m[0].map((_,x)=>m.map(_=>m[y++%h][x],h=m.length,y=x&1||h-1)))(g(m))

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

コメント済み

m => (                 // m[] = input matrix
  g = m =>             // g is the main helper function taking a matrix m[]
    m[0].map((_, x) => // for each column at position x in m[]:
      m.map(_ =>       //   for each row of m[]:
        m[y++ % h][x], //     yield the x-th value of the row (y mod h) and increment y
        h = m.length,  //     h = number of rows
        y = x & 1      //     start with y = 1 if x is odd,
            || h - 1   //     or h - 1 if x is even
      )                //   end of inner map()
  )                    // end of outer map()
)(g(m))                // invoke g twice on the input matrix




0

APL NARS、36バイト、18文字

c←b∘b←{⍵⌽⍨-×-\⍳≢⍵}∘⍉

この{⍵⌽⍨-×-\⍳≢⍵}は、行列引数の各行をベクトル-1 1 -1 1など(ベクトルの長さが引数行列行の長さになる)に従って回転させます。テスト:

  ⎕←a←3 5⍴⎕A
ABCDE
FGHIJ
KLMNO
  ⎕←c a
OKGMI
LCNEA
JFBHD


0

bash et al、84

競合しないシェルソリューション。

これは、行の回転方向を交互に切り替える関数に基づいています。転置された配列で同じ手順を実行すると、列が回転します。たとえばtranspose | rotate | transpose | rotate

交互回転は、次のように単一の文字配列で実行できますsed

sed -E 's/(.*) (.)$/\2 \1/; n; s/^(.) (.*)/\2 \1/'

転置はrsまたはで実行できますdatamash

rs -g1 -T
datamash -t' ' transpose

まとめて:

r() { sed -E 's/(.*) (.)$/\2 \1/; n; s/^(.) (.*)/\2 \1/'; }
t() { rs -g1 -T; }
<f t | r | t | r

出力:

o k g m i
l c n e a
j f b h d
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.