The Challenge
Given a n x n
matrix of integers with n >= 2
1 2 3 4
and a list of integers with exactly 2n
elements
[1,2,-3,-1]
output the rotated matrix. This matrix is contructed in the following way:
- Take the first integer in the list and rotate the first row to the right by this value.
- Take the next integer and rotate the first column down by this value.
- Take the next integer and rotate the second row to the right by this value, etc. until you rotated every row and column of the matrix once.
The list can contain negative integers which means that you shift the row/column left/up instead of right/down. If the integer is zero, don't rotate the row/column.
Example using the input above
List element Matrix Explanation ------------------------------------------------------------ 1 2 1 Rotate 1st row right by 1 3 4 2 2 1 Rotate 1st column down by 2 3 4 -3 2 1 Rotate 2nd row left by 3 4 3 -1 2 3 Rotate 2nd column up by 1 4 1
Rules
- You may choose the most conventient input format. Just make it clear which one you use.
- Function or full program allowed.
- Default rules for input/output.
- Standard loopholes apply.
- This is code-golf, so lowest byte-count wins. Tiebreaker is earlier submission.
Test cases
Input format here is a list of lists for the matrix and a normal list for the integers.
[[1,2],[3,4]],[1,2,-3,-1] -> [[2,3],[4,1]] [[1,2],[3,4]],[1,1,1,1] -> [[3,2],[4,1]] [[1,2],[3,4]],[0,0,0,0] -> [[1,2],[3,4]] [[1,2,-3],[-4,5,6],[7,-8,0]],[1,-2,0,-1,3,4] -> [[7,5,0],[-3,-8,2],[-4,1,6]] [[1,2,-3],[-4,5,6],[7,-8,0]],[3,12,-3,0,-6,-3] -> [[1,2,-3],[-4,5,6],[7,-8,0]]
Happy Coding!
-
\$\begingroup\$ mapcar should help... \$\endgroup\$msh210– msh2102016年03月06日 06:33:15 +00:00Commented Mar 6, 2016 at 6:33
4 Answers 4
CJam, 13 bytes
{{a.m>1m<z}/}
An unnamed block (function) that takes the matrix and the list on top of the stack (in that order) and leaves the new matrix in their place.
Same idea, same byte count, different implementations:
{{\(@m>a+z}/}
{{(Im>a+z}fI}
{{:\Im>]z}fI}
Explanation
Ideally we want to treat each instruction in the list the same, and just use it to rotate the first row of the matrix. This can be done quite easily by transforming the matrix after each instruction a bit and making sure that all those extra transformations cancel out in the end. So after processing each instruction, we rotate all rows one up (such that the next instruction along the same dimension processes the next row) and then transpose the matrix, such that we're actually processing columns next. These additional transformations are orthogonal to the instructions in the list and have a period of exactly 2n
, just what we need.
As for the code:
{ e# For each instruction...
a e# Wrap it in a singleton array.
.m> e# Combine it element-wise with the matrix to rotate right. This is
e# a fairly common idiom to apply a binary operation only to the first
e# element of an array, since element-wise operations just retain all the
e# unpaired elements of the longer array.
1m< e# Rotate the rows one up.
z e# Transpose.
}/
APL (Dyalog Extended), (削除) 17 (削除ここまで) (削除) 15 (削除ここまで) (削除) 14 (削除ここまで) 13 bytes
-3 bytes by Adám
(⍉1⊖⌽`@1⍢⌽)/⌽
Takes input as a list where the first element is the matrix, and the remaining elements are the rotation amounts. If ⌽ rotated to the right instead of left, this would beat CJam.
(⍉1⊖⌽@1 1⍢⌽)/⌽ Monadic train:
(⍉1⊖⌽@1 1⍢⌽) Helper function to rotate and transpose once.
Takes args ⍺ (amount to rotate) and ⍵ (current array)
⌽ Function to rotate left
1 1 2-element vector containing 1.
The second 1 is redundant, but saves 1 byte over (,1).
⌽@1 1 Function to rotate the 1st row left by ⍺.
⌽@1 1⍢⌽ Reverse ⍵, rotate 1st row left by ⍺, then reverse again.
This rotates the first row of ⍵ to the *right*.
1⊖ Rotate all the rows upward,
⍉ then transpose.
(⍉1⊖⌽@1 1⍢⌽)/⌽ Fold (/) this function over the reversed input.
If our input is ⍵, ⍺_1, ⍺_2, ..., ⍺_2n,
the reversed input will be ⍺_2n, ..., ⍺_1, ⍵.
The / operator applies the function right to left,
so the ⌽ is necessary.
-
\$\begingroup\$
{⍉1⊖⌽⍺⌽@(⍳1)⌽⍵}
→(⍉1⊖⌽@(⍳1)⍢⌽)
but can you explain to me why@1
won't work instead of@(⍳1)
or@(,1)
? Also, OP may allow you to take input in reverse. \$\endgroup\$Adám– Adám2019年02月11日 10:51:28 +00:00Commented Feb 11, 2019 at 10:51 -
\$\begingroup\$ So it turns out that dfns'
at
which is behind the extended@
isn't a compatible extension. However, you can use the native@
with`@1
which saves a byte over@1 1
. \$\endgroup\$Adám– Adám2019年02月12日 21:36:09 +00:00Commented Feb 12, 2019 at 21:36 -
\$\begingroup\$ does rotate work differently in apl vs J ? i’m trying to translate this to J and finding it a lot more verbose... \$\endgroup\$Jonah– Jonah2019年02月16日 20:32:00 +00:00Commented Feb 16, 2019 at 20:32
Python 2, 96 bytes
def f(m,v):
for i,x in enumerate(v):x%=len(m);r=m[i/2];m[i/2]=r[-x:]+r[:-x];m=zip(*m)
return m
f
returns a list of tuples. Each line in the function body is indented with 1 tab character.
-
\$\begingroup\$ Wouldn't a straight
return zip(*m)
save you 5 bytes? \$\endgroup\$Denker– Denker2016年03月06日 21:42:02 +00:00Commented Mar 6, 2016 at 21:42 -
\$\begingroup\$ @DenkerAffe: are you suggesting to replace
m=zip(*m);return m
with justreturn zip(*m)
? I can't do that becausem=zip(*m)
is a part of thefor
loop \$\endgroup\$vaultah– vaultah2016年03月07日 14:55:02 +00:00Commented Mar 7, 2016 at 14:55 -
\$\begingroup\$ Yea, that was my though. Didn't see that its in the loop, my bad. \$\endgroup\$Denker– Denker2016年03月07日 15:20:29 +00:00Commented Mar 7, 2016 at 15:20