Two sum: Difference between revisions

1,396 bytes added ,  2 years ago
Added Quackery.
(Updated to work with Nim 1.4. Also minor formatting changes.)
(Added Quackery.)
Line 1,957:
<pre>[(1, 3), (2, 5)]
[]</pre>
 
=={{header|Quackery}}==
 
So… I initially misread the task as "return the two integers" and then realised it was "…the indices of…", but that's OK — it just meant writing an extra word to find the indices, given the numbers.
 
The last three lines of <code>task</code> are in case the two integers found by <code>twosome</code> are equal - in which case, as <code>find</code> finds the first instance in the array and the array is sorted, we can safely take the index plus one as the second index.
 
<lang Quackery> [ 0 peek ] is first ( [ --> n )
 
[ -1 peek ] is last ( [ --> n )
 
[ 1 split nip ] is top ( [ --> [ )
 
[ -1 split drop ] is tail ( [ --> [ )
 
[ temp put
[ dup size 2 < iff
[ drop [] ] done
dup first over last +
temp share -
dup 0 = iff
[ drop dup first
swap last join ] done
0 < iff top else tail
again ]
temp release ] is twosum ( [ n --> [ )
 
[ over temp put
twosum
[] swap
witheach
[ temp share find join ]
temp release
dup [] != if
[ dup unpack = if
[ behead 1+ join ] ] ] is task ( [ n --> [ )
 
' [ 0 2 11 19 20 ] 21 task echo cr
' [ 0 2 11 19 20 ] 25 task echo cr
' [ 0 2 12 12 20 ] 24 task echo cr</lang>
 
{{out}}
 
<pre>[ 1 3 ]
[ ]
[ 2 3 ]
</pre>
 
=={{header|Racket}}==
1,462

edits