钻石化矩阵

20

``````1 2 3
4 5 6
7 8 9
``````

``````  1
4 2
7 5 3
8 6
9
``````

测试用例

``````Input:  [[1]]
Output: [[1]]

Input:  [[1,2],[3,4]]
Output: [[1],[3,2],[4]]

Input:  [[1,2,3],[4,5,6]]
Output: [[1],[4,2],[5,3],[6]]

Input:  [[11,2,5],[3,99,3],[4,8,15],[16,23,42]]
Output: [[11],[3,2],[4,99,5],[16,8,3],[23,15],[42]]
``````

计分

Martin Ender 16'Apr

19

J，7个字节

``````<@|./.
``````

``````   input =. i.3 4
input
0 1  2  3
4 5  6  7
8 9 10 11

<@|./. input
┌─┬───┬─────┬─────┬────┬──┐
│0│4 1│8 5 2│9 6 3│10 7│11│
└─┴───┴─────┴─────┴────┴──┘
``````

说明

• `/.`是J的内置函数，可将功能应用于每个反对角线。不幸的是，这些反对角线是按照我们想要的相反顺序给出的。
• 在中`<@|.`，我们首先应用`|.`反转反对角线，然后`<`将其装箱（这是在J中返回一个参差不齐的数组的唯一方法，因为法线数组始终是矩形，所以反对角线将用零填充）。

5

Python，91字节

``````e=enumerate
lambda M:[[r[n-i]for i,r in e(M)if-1<n-i<len(r)][::-1]for n,_ in e(M[1:]+M[0])]
``````

Ideone上进行测试

Python + NumPy，69个字节

``````import numpy
lambda M:map(M[::-1].diagonal,range(1-len(M),len(M[0])))
``````

4

果冻，7个字节

``````ṚŒDṙZL\$
``````

说明

``````Ṛ         Reverse the matrix vertically.
ŒD       Get its diagonals. However these start from
the main diagonal, not the corners.
ZL\$   Get the width of the input matrix.
ṙ      Rotate the list of diagonals left by that many
places to obtain the correct order.
``````

Guidobot

5
@Guidobot Jelly使用一个自定义代码页，它将其理解为一个字节的256个字符中的每一个编码。

4

Mathematica，58 56个字节

``````a=Length;Reverse@#~Diagonal~b~Table~{b,1-a@#,a@#&@@#-1}&
``````

Sp3000

`Length@#&@@#`仅对于相同字节数的ASCII。
Martin Ender

3

CJam，17个字节

``````{eeSf.*::+W%zSf-}
``````

``````{_,,Sf*\.+W%zSf-}
``````

说明

``````[[0  1  2  3]
[4  5  6  7]
[8  9 10 11]]

ee    e# Enumerate matrix, turning each row [x ... z] into [i [x ... z]] where
e# i is the vertical index from the top.

[[0 [0  1  2  3]]
[1 [4  5  6  7]]
[2 [8  9 10 11]]]

Sf.*  e# Replace each i with a string of i spaces.

[[""   [0  1  2  3]]
[" "  [4  5  6  7]]
["  " [8  9 10 11]]]

::+   e# Prepend these strings to the rows.

[[0  1  2  3]
['  4  5  6  7]
['  '  8  9 10 11]]   e# Note that each '  corresponds to a space character.

W%    e# Reverse the rows.

[['  '  8  9 10 11]
['  4  5  6  7]
[0  1  2  3]]

z     e# Zip/transpose.

[[ '  '  0]
[ '  4  1]
[ 8  5  2]
[ 9  6  3]
[10  7]
[11]]

Sf-   e# Remove spaces from each row.

[[ 0]
[ 4  1]
[ 8  5  2]
[ 9  6  3]
[10  7]
[11]]
``````

3

Python 2，88 87字节

``lambda L:[filter(None,x)[::-1]for x in map(None,[],*[i*[0]+r for i,r in enumerate(L)])]``

（感谢@Dennis提供了-1个字节）

3

Ruby，68 66个字节

``````->l{i=-1;k=[];l.map{|r|i-=j=-1;r.map{|e|k[i+j+=1]=[e,*k[i+j]]}};k}
``````
• 由于splat运算符的工作原理，通过放弃数组添加，我能够节省2个字节。

2

Mathematica，60个字节

``````#&@@@#&/@GatherBy[Join@@MapIndexed[List,#,{2}],Tr@*Last]&
``````

说明

``````MapIndexed[List,#,{2}]
``````

``````Join@@...
``````

``````GatherBy[..., Tr@*Last]
``````

``````#&@@@#&/@...
``````

Fatalize 2016年

1
@Fatalize这是一个私人使用的Unicode代码点，与该代码点关联的标志符号Mathematica是上标`T`reference.wolfram.com/language/ref/character/Transpose.html ... `\[Transpose]`仅是该Unicode字符的ASCII音译。将Unicode字符或音译复制到Mathematica中将起作用。
Martin Ender

2

八度，77字节

``````@(M)char(accumarray(((1:size(M,1))+(0:size(M,2)-1)')(:),M(:),[],@(x){num2str(x')}))
``````

``````@(M)char(accumarray(((1:size(M,1))+(0:size(M,2)-1)')(:),M(:),[],@(x){num2str(x')}))
``````

2

JavaScript（Firefox），86 75字节

``a=>a.concat(a[0]).slice(1).map((_,i)=>[for(v of a)if(n=v[i--])n].reverse())``

@Neil节省了11个字节！

@Neil Wow，我不了解使用`concat`和有点傻`slice`。谢谢！
user81655 '16

2

八度，63 62字节

``````@(a)cellfun(@(x)x(x>0)',num2cell(spdiags(flipud(a)),1),'un',0)
``````

Luis Mendo

@LuisMendo为什么，是的，我可以！谢谢！:)

2

``````r=zip[0..]
\o->fst\$span(any(>0))[reverse[e|(x,t)<-r o,(v,e)<-r t,x+v==a]|a<-[0..]]
``````

1

Python，128个字节（numpy）

``````(lambda A: (lambda A,S:[[A[U][I-U] for U in range(min(S[1]-1,I),max(I-S[0]+1,0)-1,-1)] for I in range(S[1]+S[0]-1)])(A,A.shape))
``````

Alex A.

`lambda A:[[A[U][I-U]for U in range(max(I-len(A)+1,0),min(len(A[0])-1,I)+1)]for I in range(len(A+A[0])-1)]`（与您的原始版本一样）会短一些。另外，您应该更改`A[U][I-U]``A[I-U][U]`以从问题中获得方向。

Luis Masuelli '16

1

Pyth，41 17字节

``````tm_<dx+dYk.T+LaYk
``````

``````tm_<dx+dYk.T+LaYk
+L      prepend to each subarray...
aYk   (Y += ''). Y is initialized to [],
so this prepends [''] to the first
subarray, ['', ''] to the second, etc.
['' 1  2  3
'' '' 4  5  6
'' '' '' 7  8  9
'' '' '' '' 10 11 12
'' '' '' '' '' 13 14 15]
.T        transpose, giving us
['' '' '' '' ''
1  '' '' '' ''
2  4  '' '' ''
3  5  7  '' ''
6  8  10 ''
9  11 13
12 14
15]
m_<dx+dYk          removes all empty strings in the
subarrays while reversing each one
t                   remove the first subarray
``````

``````JlQKlhQm_m@@Qk-dk}h.MZ,0-dtKh.mb,tJdUt+JK
``````

``````JlQKlhQm_m@@Qk-dk}h.MZ,0-dtKh.mb,tJdUt+JK    input array stored as Q
JlQ                                          J = len(Q)
KlhQ                                      K = len(Q[0])
m                            Ut+JK    list for d from 0 to J+K-1:
_m       }AAAAAAAAAABBBBBBBB             reversed list for k from A to B, where:
h.MZ,0-dtK                       A = max(0, d-(K-1))
0-dtK                               0  d-(K-1)
h.mb,tJd               B = min(J-1, d)
tJd                       J-1  d
@@Qk-dk                                    Q[k][d-k]
``````

1

Groovy，77 73 75

``````{i->o=[].withDefault{[]};a=0;i.each{b=0;it.each{o[a+b++].add(0,it)};a++};o}
``````