Two sum: Difference between revisions
m
→Functional: tweaks
SqrtNegInf (talk | contribs) m (→Functional: include one-line version of code, to clarify left/right) |
SqrtNegInf (talk | contribs) m (→Functional: tweaks) |
||
Line 1,493:
Both return all pairs that sum to the target value, not just the first (e.g. for input of <code>0 2 10 11 19 90</code> would get indices 1/4 and 2/3).
<lang perl6>sub two-sum-lr (@a, $sum) {
# (((^@a X ^@a) Z=> (@a X+ @a)).grep($sum == *.value)>>.
(
(
Line 1,503:
sub two-sum-rl (@a, $sum) {
# unique map {.split(' ').sort.join(' ')}, keys %(grep {.value == $sum},
unique
map {.split(' ').sort.join(' ')},
keys %(
grep {.value == $sum},
(^@a X ^@a) Z=> (@a X+ @a)
)
|