Two sum: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(add RPL)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 1,055:
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 3,041 ⟶ 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,068 ⟶ 3,061:
target_sum := 21
p1, p2, ok := two_sum(a, target_sum)
if !ok {println("TheNo two numbers withwere indicesfound $p1 and $p2whose sum tois $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,128 ⟶ 3,119:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var twosum = Fn.new { |a, n|
var c = a.count
if (c < 2) return []
Line 3,234 ⟶ 3,225:
<pre>The numbers with indices 2 and 4 sum to 21
---Program done, press RETURN---</pre>
=={{header|zigZig}}==
<syntaxhighlight lang="zig">
 
'''Works with:''' 0.11.x, 0.12.0-dev.1389+42d4d07ef
const std = @import("std");
const print = @import("std").debug.print;
const retour = struct {
ok: bool,index1: usize, index2: usize
};
 
<syntaxhighlight lang="zig">pub fn sumsUpTo(comptime T: type, input: []const T, target_sum: T) ?struct { usize, usize } {
pub fn main() !void {
if (input.len <= 1) return null;
const array1 = [_]usize {0, 2, 11, 19, 90};
const Res = searchTwoNumbers(&array1,21);
if (Res.ok)
print("Result= [ {},{} ] \n", .{ Res.index1,Res.index2 })
else
print("No find ! \n",.{});
 
return result: for (input[0 .. input.len - 1], 0..) |left, left_i| {
//*****************************
if (left > target_sum) break :result null;
// search two numbers for sum
 
//*****************************
const sum1: usizeoffset = valueleft_i + value11;
fn searchTwoNumbers(a: []const usize,sum: usize) retour {
for (ainput[offset..], 0offset..) |value1right, jright_i| {
var ret = retour{
.ok const current_sum = false,left + right;
if (current_sum < target_sum) continue;
.index1 = 0,
if (current_sum == target_sum) break :result .{ left_i, right_i };
.index2 = 0,
if sum(current_sum > target_sum) {break;
};
for (a, 0..) |value, i| {}
} else {null;
for (a, 0..) |value1, j| {
}</syntaxhighlight>
const sum1: usize = value + value1;
 
if (sum1 == sum) {
<syntaxhighlight lang="zig">const std = @import("std");
ret.ok = true;
 
ret.index1=i;
pub fn main() std.fs.File.WriteError!void {
ret.index2=j;
const stdout = return retstd.io.getStdOut();
const stdout_w = stdout.writer();
}
 
}
const stderr = std.io.getStdErr();
}
const stderr_w = stderr.writer();
 
return ret;
const array1a = [_]usize 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", .{ Res.index1indexes[0],Res.index2 indexes[1] });
} else }{
try stderr_w.print("Numbers with sum {d} were not found!\n", .{target_sum});
}
}</syntaxhighlight lang="zig">
 
</syntaxhighlight>
{{Out}}
<pre>
Resultat=Result: [ 1, 3 ].
</pre>
 
9,476

edits