Two sum: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(One intermediate revision by one other user not shown) | |||
Line 3,119:
=={{header|Wren}}==
<syntaxhighlight lang="
var c = a.count
if (c < 2) return []
Line 3,225:
<pre>The numbers with indices 2 and 4 sum to 21
---Program done, press RETURN---</pre>
=={{header|
<syntaxhighlight lang="zig">▼
'''Works with:''' 0.11.x, 0.12.0-dev.1389+42d4d07ef
const std = @import("std");▼
<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};▼
print("Result= [ {},{} ] \n", .{ Res.index1,Res.index2 })▼
else▼
return result: for (input[0 .. input.len - 1], 0..) |left, left_i| {
if (left > target_sum) break :result null;
if (current_sum < target_sum) continue;
if (current_sum == target_sum) break :result .{ left_i, right_i };
};▼
} else null;
▲ for (a, 0..) |value1, j| {
▲ const sum1: usize = value + value1;
▲ if (sum1 == sum) {
▲<syntaxhighlight lang="zig">const std = @import("std");
▲pub fn main() std.fs.File.WriteError!void {
const stdout =
const stdout_w = stdout.writer();
const stderr = std.io.getStdErr();
▲ }
const stderr_w = stderr.writer();
const target_sum: u32 = 21;
const optional_indexes = sumsUpTo(u32, &a, target_sum);
if (optional_indexes) |indexes| {
▲ } else {
try stderr_w.print("Numbers with sum {d} were not found!\n", .{target_sum});
}</syntaxhighlight>▼
▲</syntaxhighlight>
{{Out}}
<pre>
</pre>
|