Alternatively shift columns and rows of a 2D array
MATL, 13 bytes
,!tZy:oEq2&YS
Try it online!
Explanation
, % 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)
J, 26, 21 19 bytes
-5 bytes thanks to miles
(|."_1~_1^#\)@|:^:2
Explanation:
^:2
- repeate twice the following:
@|:
- transpose and
#\
- find the length ot the prefixes (1, 2, 3 ... rows)
_1^
- raise -1 to the above powers, creating a list of alternating -1 1 -1 1...
|."_1~
- rotate each row of the input array with offset from the above list
Try it online!
Original version:
(($_1 1"0)@#|."0 1])@|:^:2
How it works
^:2
- repeate twice the following:
|:
- transpose and
|."0 1]
- rotate each row of the input array, offsets in the list:
@#
- the number of rows in the array
($_1 1"0)
- alternate _1 1 (3 -> _1 1 _1)
Try it online!
Wolfram Language (Mathematica), 55 bytes
Nest[MapIndexed[RotateLeft[#,(-1)^#2]&,Thread@#]&,#,2]&
Try it online!