Two sum: Difference between revisions

Added APL solution
(→‎{{header|jq}}: stickler)
(Added APL solution)
Line 106:
[]
[2, 3]
</pre>
 
=={{header|APL}}==
Works with [[Dyalog APL]].
 
∘.+⍨ ⍺ makes a table that is the outer sum of the left argument (the numbers).
 
We want to remove the diagonal, to avoid edge cases. We can achieve this by setting all these numbers to an arbitrary decimal value, since two integers can't sum to a decimal.
 
≢⍺ is the length of the numbers. ⍳≢⍺ creates an array from 0 to the length of the numbers. ∘.=⍳≢⍺ returns an identity matrix of size ≢⍺
(using the outer product with the equality function). ⍸ returns the indices of these numbers. 0.1@ sets that list to 0.1.
 
Then, we just need to find where the right argument (the target) is equal to the matrix, get the indices, and return the first one (⊃).
<lang APL>
⎕io ← 0 ⍝ sets index origin to 0
ts ← {⊃⍸ ⍵= 0.1@(⍸∘.=⍨⍳≢⍺) ∘.+⍨ ⍺}
⎕ ← 0 2 11 19 90 ts 21 ⍝ should be 1 3
</lang>
{{out}}
<pre>
1 3
</pre>
 
Anonymous user