J,18个字节
>:(,&|:|."{,)i.3 3
在线尝试!
输出量
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 3 4 5 6 7 8 9 1
5 6 7 8 9 1 2 3 4
8 9 1 2 3 4 5 6 7
3 4 5 6 7 8 9 1 2
6 7 8 9 1 2 3 4 5
9 1 2 3 4 5 6 7 8
怎么运行的
>:(,&|:|."{,)i.3 3
i.3 3 The 2D array X = [0 1 2;3 4 5;6 7 8]
,&|:|."{, 3-verb train:
,&|: Transpose and flatten X to get Y = [0 3 6 1 4 7 2 5 8]
, Flatten X to get Z = [0 1 2 3 4 5 6 7 8]
|."{ Get 2D array whose rows are Z rotated Y times
>: Increment
花式版本,23字节
|.&(>:i.3 3)&.>|:{;~i.3
在线尝试!
输出:
┌─────┬─────┬─────┐
│1 2 3│4 5 6│7 8 9│
│4 5 6│7 8 9│1 2 3│
│7 8 9│1 2 3│4 5 6│
├─────┼─────┼─────┤
│2 3 1│5 6 4│8 9 7│
│5 6 4│8 9 7│2 3 1│
│8 9 7│2 3 1│5 6 4│
├─────┼─────┼─────┤
│3 1 2│6 4 5│9 7 8│
│6 4 5│9 7 8│3 1 2│
│9 7 8│3 1 2│6 4 5│
└─────┴─────┴─────┘
怎么运行的
|.&(>:i.3 3)&.>|:{;~i.3
i.3 Array [0 1 2]
{;~ Get 2D array of boxed pairs (0 0) to (2 2)
|: Transpose
|.&(>:i.3 3)&.> Change each pair to a Sudoku box:
&.> Unbox
>:i.3 3 2D array X = [1 2 3;4 5 6;7 8 9]
|.& Rotate this 2D array over both axes
e.g. 1 2|.X gives [6 4 5;9 7 8;3 1 2]
&.> Box again so the result looks like the above