Two sum: Difference between revisions

3,120 bytes added ,  3 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
(add task to aarch64 assembly for raspberry 3)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(7 intermediate revisions by 6 users not shown)
Line 1:
{{draftDraft task|Arithmetic operations}}
 
 
Line 1,052:
EasyLang arrays are one-based, so the indices returned are also one-based.
<syntaxhighlight lang="easylang">
funcproc twoSum sum . array[] pair[] .
i = 1
j = len array[]
# The array remains empty if no sum is found
pair[] = [ ]
repeat
if array[i] + array[j] = sum
pair[] = [ i j ]
break 2return
elif array[i] + array[j] > sum
j -= 1
Line 1,070 ⟶ 1,069:
.
numbers[] = [ 0 2 11 19 90 ]
call twoSum 21 numbers[] pair[]
print "[" & pair[1] & ", " & pair[2] & "]"
</syntaxhighlight>
{{out}}
<pre>[ 2, 4]</pre>
 
=={{header|Elixir}}==
Line 1,139 ⟶ 1,138:
{ 0 2 }
</pre>
 
A version that maintains a point-free style while still iterating over the numbers once:
 
<syntaxhighlight lang="factor">USING: accessors arrays assocs combinators.extras hashtables
kernel math math.combinatorics sequences ;
IN: rosetta-code.two-sum
 
DEFER: (two-sum)
TUPLE: helper sum seq index hash ;
 
: <two-sum-helper> ( sum seq -- helper )
\ helper new
swap [ >>seq ] keep length <hashtable> >>hash
swap >>sum 0 >>index ;
 
: no-sum ( helper -- empty ) drop { } ;
 
: in-bounds? ( helper -- ? )
[ index>> ] [ seq>> length ] bi < ;
 
: next-sum ( helper -- pair )
dup in-bounds? [ (two-sum) ] [ no-sum ] if ;
 
: next-index ( helper -- helper ) [ 1 + ] change-index ;
 
: result ( helper index -- helper ) swap index>> 2array ;
 
: find-compliment-index ( helper -- helper index/f )
dup [ sum>> ] [ index>> ] [ seq>> nth - ] [ ] quad hash>> at ;
 
: remember-item ( helper -- helper )
dup [ hash>> ] [ index>> ] [ seq>> nth ] [ index>> ]
quad set-of drop ;
 
: (two-sum) ( helper -- pair )
remember-item find-compliment-index
[ result ] [ next-index next-sum ] if* ;
 
: two-sum ( sum seq -- pair ) <two-sum-helper> (two-sum) ;
 
MAIN: [ { 21 55 11 } [ { 0 2 11 19 90 } two-sum . ] each ]
</syntaxhighlight>
 
=={{header|Forth}}==
Line 2,756 ⟶ 2,797:
target sum: 21
a solution: [1 3]
</pre>
 
=={{header|RPL}}==
≪ → array sum
≪ { }
1 array SIZE '''FOR''' j
array j 0 PUT
sum array j GET -
'''IF''' POS '''THEN'''
j LAST
'''IF''' DUP2 > '''THEN''' SWAP '''END'''
R→C
'''IF''' DUP2 POS '''THEN''' DROP '''ELSE''' + '''END'''
'''END NEXT'''
≫ ≫ ‘<span style="color:blue">TWOSUM</span>’ STO
 
{0 2 11 19 90} 21 <span style="color:blue">TWOSUM</span>
{0 2 11 19 90} 22 <span style="color:blue">TWOSUM</span>
{0 2 3 3 4 11 17 17 18 19 90} 21 <span style="color:blue">TWOSUM</span>
{{out}}
<pre>
3: { (2,4) }
2: { }
1: { (2,10) (3,9) (4,9) (5,7) (5,8) }
</pre>
 
Line 2,975 ⟶ 3,040:
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn two_sum(a []int, target_sum int) (int, int, bool) {
fn two_sum(a []int, target_sum int) (int, int, bool) {
len := a.len
if len < 2 {return 0, 0, false}
return 0, 0, false
}
for i in 0..len - 1 {
if a[i] <= target_sum {
for j in i + 1..len {
sum := a[i] + a[j]
if sum == target_sum {return i, j, true}
if sum > target_sum return i, j, true{break}
}
if sum > target_sum {
break
}
}
} else {
else {break}
}
}
return 0, 0, false
Line 3,002 ⟶ 3,061:
target_sum := 21
p1, p2, ok := two_sum(a, target_sum)
if !ok {println("No two numbers were found whose sum is $target_sum")}
if !ok {
else {println("No twoThe numbers werewith foundindices whose$p1 and $p2 sum isto $target_sum")}
}
} else {
</syntaxhighlight>
println("The numbers with indices $p1 and $p2 sum to $target_sum")
}
}</syntaxhighlight>
 
{{out}}
Line 3,062 ⟶ 3,119:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var twosum = Fn.new { |a, n|
var c = a.count
if (c < 2) return []
Line 3,168 ⟶ 3,225:
<pre>The numbers with indices 2 and 4 sum to 21
---Program done, press RETURN---</pre>
=={{header|Zig}}==
 
'''Works with:''' 0.11.x, 0.12.0-dev.1389+42d4d07ef
 
<syntaxhighlight lang="zig">pub fn sumsUpTo(comptime T: type, input: []const T, target_sum: T) ?struct { usize, usize } {
if (input.len <= 1) return null;
 
return result: for (input[0 .. input.len - 1], 0..) |left, left_i| {
if (left > target_sum) break :result null;
 
const offset = left_i + 1;
for (input[offset..], offset..) |right, right_i| {
const current_sum = left + right;
if (current_sum < target_sum) continue;
if (current_sum == target_sum) break :result .{ left_i, right_i };
if (current_sum > target_sum) break;
}
} else null;
}</syntaxhighlight>
 
<syntaxhighlight lang="zig">const std = @import("std");
 
pub fn main() std.fs.File.WriteError!void {
const stdout = std.io.getStdOut();
const stdout_w = stdout.writer();
 
const stderr = std.io.getStdErr();
const stderr_w = stderr.writer();
 
const a = [_]u32{ 0, 2, 11, 19, 90 };
const target_sum: u32 = 21;
 
const optional_indexes = sumsUpTo(u32, &a, target_sum);
if (optional_indexes) |indexes| {
try stdout_w.print("Result: [{d}, {d}].\n", .{ indexes[0], indexes[1] });
} else {
try stderr_w.print("Numbers with sum {d} were not found!\n", .{target_sum});
}
}</syntaxhighlight>
 
{{Out}}
<pre>
Result: [1, 3].
</pre>
 
=={{header|zkl}}==
9,485

edits