Jump to content

Two sum: Difference between revisions

m
m (→‎Functional: include one-line version of code, to clarify left/right)
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)>>.keykeys.map:{ .split(' ').sort.join(' ')}).unique
(
(
Line 1,503:
 
sub two-sum-rl (@a, $sum) {
# unique map {.split(' ').sort.join(' ')}, keys %(grep {.value == $sum}, %((^@a X ^@a) Z=> (@a X[+] @a)))
unique
map {.split(' ').sort.join(' ')},
keys %(
grep {.value == $sum}, %(
(^@a X ^@a) Z=> (@a X+ @a)
)
2,392

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.