diff options
| author | Util <bruce.gray@acm.org> | 2024-01-04 19:24:28 -0600 |
|---|---|---|
| committer | Util <bruce.gray@acm.org> | 2024-01-04 19:24:28 -0600 |
| commit | 93e56095a57114a5bfd1850348fbaf0af3e959bb (patch) | |
| tree | c6c75ef0c4b9be8fdb781f0a35990fa912e8e224 | |
| parent | 5f38c976cae9103ec02e413224d047d8b149956d (diff) | |
| download | perlweeklychallenge-club-93e56095a57114a5bfd1850348fbaf0af3e959bb.tar.gz perlweeklychallenge-club-93e56095a57114a5bfd1850348fbaf0af3e959bb.tar.bz2 perlweeklychallenge-club-93e56095a57114a5bfd1850348fbaf0af3e959bb.zip | |
Add TWC 250 solutions by Bruce Gray (in Raku and Zig).
| -rw-r--r-- | challenge-250/bruce-gray/raku/ch-1.raku | 16 | ||||
| -rw-r--r-- | challenge-250/bruce-gray/raku/ch-2.raku | 14 | ||||
| -rw-r--r-- | challenge-250/bruce-gray/zig/ch-1.zig | 19 | ||||
| -rw-r--r-- | challenge-250/bruce-gray/zig/ch-2.zig | 23 |
4 files changed, 72 insertions, 0 deletions
diff --git a/challenge-250/bruce-gray/raku/ch-1.raku b/challenge-250/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..c60fb35f5f --- /dev/null +++ b/challenge-250/bruce-gray/raku/ch-1.raku @@ -0,0 +1,16 @@ +sub task1 ( @ns --> Int ) { + return @ns.keys.first({ ($_ mod 10) == @ns[$_] }) // -1; +} + + +constant @tests = + ( 0, (0, 1, 2) ), + ( 2, (4, 3, 2, 1) ), + ( -1, (1, 2, 3, 4, 5, 6, 7, 8, 9, 0) ), + + ( 11, (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1) ), +; +use Test; plan +@tests; +for @tests -> ( $expected, @in ) { + is task1(@in), $expected; +} diff --git a/challenge-250/bruce-gray/raku/ch-2.raku b/challenge-250/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..8f22f21263 --- /dev/null +++ b/challenge-250/bruce-gray/raku/ch-2.raku @@ -0,0 +1,14 @@ +sub task2 ( @a --> Int ) { + return @a.map({ +$_ // .chars }).max; +} + + +constant @tests = + ( 6, <perl 2 000 python r4ku> , 'Task example 1' ), + ( 1, <001 1 000 0001> , 'Task example 2' ), + ( -3, <-3 -55 -987> , 'Max is negative' ), +; +use Test; plan +@tests; +for @tests -> ( $expected, @in, $name ) { + is task2(@in), $expected, $name; +} diff --git a/challenge-250/bruce-gray/zig/ch-1.zig b/challenge-250/bruce-gray/zig/ch-1.zig new file mode 100644 index 0000000000..eb52468fab --- /dev/null +++ b/challenge-250/bruce-gray/zig/ch-1.zig @@ -0,0 +1,19 @@ +// Run as: `zig test zig/ch-1.zig` +const std = @import("std"); + +fn task1(ns: []const i8) i8 { + for (ns, 0..) |n, index| { + if ((index % 10) == n) { + return @as(i8, @intCast(index)); + } + } + return -1; +} + + +pub fn main() void {} +const expect = std.testing.expect; +test "Task example 1" { try expect(task1(&[_]i8{ 0, 1, 2 }) == 0); } +test "Task example 2" { try expect(task1(&[_]i8{ 4, 3, 2, 1 }) == 2); } +test "Task example 3" { try expect(task1(&[_]i8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }) == -1); } +test "Breaker 1" { try expect(task1(&[_]i8{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 }) == 11); } diff --git a/challenge-250/bruce-gray/zig/ch-2.zig b/challenge-250/bruce-gray/zig/ch-2.zig new file mode 100644 index 0000000000..249da87e2f --- /dev/null +++ b/challenge-250/bruce-gray/zig/ch-2.zig @@ -0,0 +1,23 @@ +// Run as: `zig test zig/ch-2.zig` +const std = @import("std"); + +fn signed_int_or_length(word: []const u8) i64 { + return std.fmt.parseInt(i64, word, 10) + catch @as(i64, @intCast(word.len)); +} + +fn task2(words: []const []const u8) i64 { + var ret: i64 = std.math.minInt(i64); + + for (words) |word| { + ret = @max(ret, signed_int_or_length(word)); + } + + return ret; +} + +pub fn main() void {} +const expect = std.testing.expect; +test "Task example 1" { try expect(task2(&[_][]const u8 { "perl", "2", "000", "python", "r4ku" }) == 6); } +test "Task example 2" { try expect(task2(&[_][]const u8 { "001", "1", "000", "0001" }) == 1); } +test "Max is negative" { try expect(task2(&[_][]const u8 { "-3", "-55", "-987", }) == -3); } |
