답변:
×UŒDḢS
온라인으로 사용해보십시오! 또는 모든 테스트 사례를 확인
×UŒDḢS Main link. Argument: M (matrix)
U Upend M, i.e., reverse each row.
, Pair M and upended M.
ŒD Yield all diagonals.
Ḣ Head; extract the first, main diagonal.
S Reduce by sum.
t!P!*Xds
입력 형식은
[-1, -8, 4; 4, 0 -5; -3, 5, 2]
온라인으로 사용해보십시오! 또는 모든 테스트 사례를 확인하십시오 .
t % Take input matrix implicitly. Duplicate
!P! % Flip matrix horizontally
* % Element-wise product
Xd % Extract main diagonal as a column vector
s % Sum. Display implicitly
[:+/(<0 1)|:(*|."1)
직접적인 접근.
@ Lynn 덕분에 2 바이트를 절약했습니다 .
입력 배열은를 사용하여 형성됩니다 dimensions $ values
.
f =: [:+/(<0 1)|:(*|."1)
f (2 2 $ _1 1 _2 1)
_3
f (2 2 $ 824 _65 _814 _741)
549614
f (3 3 $ _1 _8 4 4 0 _5 _3 5 2)
_10
f (3 3 $ 0 _1 0 1 0 2 1 0 1)
1
[:+/(<0 1)|:(*|."1) Input: matrix M
|."1 Reverse each row of M
* Multiply element-wise M and the row-reversed M
(<0 1)|: Take the diagonal of that matrix
[:+/ Sum that diagonal and return it=
이전 솔루션을 더 좋아했지만 훨씬 짧습니다 (기본적으로 Python 솔루션과 동일 함).
f m=sum[r!!i*r!!(length m-i-1)|(i,r)<-zip[0..]m]
<:@#{+//.@:(*|."1)
( ) | Monadic hook
* | Argument times...
|."1 | The argument mirrored around the y axis
+//.@: | Make a list by summing each of the diagonals of the matrix
{ | Takes element number...
<:@# | Calculates the correct index (size of the array - 1)
í*Å\O
온라인으로 시도 하거나 모든 테스트 사례를 확인하십시오 .
설명:
í # Reverse each row of the (implicit) input-matrix
# i.e. [[-1,-8,4],[4,0,-5],[-3,5,2]] → [[4,-8,-1],[-5,0,4],[2,5,-3]]
* # Multiply it with the (implicit) input-matrix (at the same positions)
# i.e. [[-1,-8,4],[4,0,-5],[-3,5,2]] and [[4,-8,-1],[-5,0,4],[2,5,-3]]
# → [[-4,64,-4],[-20,0,-20],[-6,25,-6]]
Å\ # Get the diagonal-list from the top-left corner towards the bottom-right
# i.e. [[-4,64,-4],[-20,0,-20],[-6,25,-6]] → [-4,0,-6]
O # Sum it (and output implicitly)
# i.e. [-4,0,-6] → -10
[:+/(<0 1)|:(*|."1)
is 19 bytes