aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-05 20:57:34 +0000
committerGitHub <noreply@github.com>2024-01-05 20:57:34 +0000
commit985b747d40117104992cf0b9dcc1605ec8cc0e5d (patch)
treeb497872fa3e106f7a523480a927f6670dfc2354e
parentc244f50829c562d69aa29c2573ebd71b5e4c4a5c (diff)
parent93e56095a57114a5bfd1850348fbaf0af3e959bb (diff)
downloadperlweeklychallenge-club-985b747d40117104992cf0b9dcc1605ec8cc0e5d.tar.gz
perlweeklychallenge-club-985b747d40117104992cf0b9dcc1605ec8cc0e5d.tar.bz2
perlweeklychallenge-club-985b747d40117104992cf0b9dcc1605ec8cc0e5d.zip
Merge pull request #9349 from Util/c250
Add TWC 250 solutions by Bruce Gray (in Raku and Zig).
-rw-r--r--challenge-250/bruce-gray/raku/ch-1.raku16
-rw-r--r--challenge-250/bruce-gray/raku/ch-2.raku14
-rw-r--r--challenge-250/bruce-gray/zig/ch-1.zig19
-rw-r--r--challenge-250/bruce-gray/zig/ch-2.zig23
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); }