或者移动2D阵列的列和行


15

目的

给定任意大小的2D数组,编写程序或函数以交替移动列和行

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

第一列中的所有元素向下移动一行,第二列中的所有元素向上移动一行,第三列中的所有向下移动一行,依此类推,当它们到达边缘时会自动换行。

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

在所有元素第一行移位到右侧时,第二左侧,所述第三等,包装,当他们到达边缘。

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

我将遵循选择最短的工作代码作为最佳答案的传统


数组可以是任意大小,还是3x5?
Jo King

我一直在寻找任何填充的2D数组。抱歉没有提及。
病案

老实说,格式不正确会使问题看起来像是懒惰的SO用户提出的题外问题。
user202729 '18

(顺便说一句,不要太快接受答案)
user202729 '18

5
@kshishoo对于将来的挑战,您可以使用沙盒 检查重复的和/或收集一些反馈,然后再发布到主站点上
Rod

Answers:


3

外壳,7个字节

‼ozṙİ_T

在线尝试!

说明

‼ozṙİ_T  Implicit input: a list of lists.
‼        Do this twice:
      T   Transpose,
 oz       then zip with
    İ_    the infinite list [-1,1,-1,1,-1,1,..
   ṙ      using rotation. This rotates the rows in alternating directions.

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 -重复以下两次:

@|: -转置和

#\ -查找前缀的长度(1、2、3 ...行)

_1^ -将-1提高至上述幂,创建一个交替-1 1 -1 1 ...的列表。

|."_1~ -旋转输入数组的每一行,使其偏离上面的列表

在线尝试!

原始版本:

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

怎么运行的

^:2 -重复以下两次:

|: -转置和

|."0 1] -旋转输入数组的每一行,在列表中偏移:

@# -数组中的行数

($_1 1"0) -备用_1 1(3-> _1 1 _1)

在线尝试!


1
您可以生成_1 1..使用(|."_1~_1^2|#\)@|:^:2
英里

@miles谢谢,这是一段很棒的代码!
Galen Ivanov '18

@miles实际上,我不需要这个2|部分
Galen Ivanov

1
是的,您实际上不知道,那又节省了2个字节。
英里



2

APL + WIN,30个字节

提示输入二维数组的屏幕

((↑⍴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"

说明:

翻转格,以便施加旋转,在第二次迭代翻转再次从而转动被施加到上的第二遍。

旋转基于-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


1

干净,93字节

import StdEnv,StdLib
k=[0,1:k]
^l=[[[last a:init a],tl a++[hd a]]!!b\\a<-transpose l&b<-k]

^o^

作为部分函数文字,它恰好看起来像一张脸。

在线尝试!


0

05AB1E,14个字节

2FøvyNÉiÀëÁ}})

在线尝试!

说明

2F               # 2 times do:
  ø              # zip
   vy            # for each row(y), index(N) do:
     NÉiÀ        # if N is odd, rotate left
         ëÁ      # else rotate right
           }}    # end if and inner loop
             )   # wrap in list


0

bash等,84

非竞争性shell解决方案。

这基于交替行旋转方向的函数。对转置数组执行的相同过程将旋转列。例如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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.