विकर्णों का डॉट उत्पाद


10

यह चुनौती बहुत सरल है। आपको इनपुट एक वर्ग मैट्रिक्स के रूप में दिया जाता है, जिसे किसी भी तरह से दर्शाया जाता है, और आपको मैट्रिक्स के विकर्णों के डॉट उत्पाद का उत्पादन करना होता है।

विशिष्ट में विकर्ण शीर्ष-बाएं से नीचे-दाएं और ऊपर-दाएं से नीचे-बाएं से चल रहे विकर्ण हैं।

परीक्षण के मामलों

[[-1, 1], [-2, 1]]  ->  -3
[[824, -65], [-814, -741]]  ->  549614
[[-1, -8, 4], [4, 0, -5], [-3, 5, 2]]  ->  -10
[[0, -1, 0], [1, 0, 2], [1, 0, 1]]  ->  1

जवाबों:



3

MATL , 8 बाइट्स

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

2

पायथन, 47 बाइट्स

lambda x:sum(r[i]*r[~i]for i,r in enumerate(x))

Ideone पर इसका परीक्षण करें ।


2

जे, 21 19 बाइट्स

[:+/(<0 1)|:(*|."1)

सीधे-आगे का दृष्टिकोण।

@ लिन के लिए 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=

[:+/(<0 1)|:(*|."1)19 बाइट्स है
लिन


1

जावास्क्रिप्ट (ईएस 6), 45 बाइट्स

a=>a.reduce((r,b,i)=>r+b[i]*b.slice(~i)[0],0)
a=>a.reduce((r,b,i)=>r+b[i]*b[b.length+~i],0)




0

क्लोजर, 57 बाइट्स

#(apply +(map(fn[i r](*(r i)(nth(reverse r)i)))(range)%))


0

जे, 18 बाइट्स

<:@#{+//.@:(*|."1)

explaination:

           (     ) | 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)

0

05AB1E , 5 बाइट्स

í*Å\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
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.