aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUtil <bruce.gray@acm.org>2024-05-05 13:04:26 -0500
committerUtil <bruce.gray@acm.org>2024-05-05 13:04:26 -0500
commit07e45bfa204c0b67feb69125f07d8c5f0b0f9938 (patch)
tree29d5487f5d6e45a89526e3f4757e2c061cb810e3
parent1bd2201ae7e0cd4456665fcbd952f001264e3459 (diff)
downloadperlweeklychallenge-club-07e45bfa204c0b67feb69125f07d8c5f0b0f9938.tar.gz
perlweeklychallenge-club-07e45bfa204c0b67feb69125f07d8c5f0b0f9938.tar.bz2
perlweeklychallenge-club-07e45bfa204c0b67feb69125f07d8c5f0b0f9938.zip
Add TWC 267 solutions by Bruce Gray, in Raku only.
-rw-r--r--challenge-267/bruce-gray/raku/ch-1.raku38
-rw-r--r--challenge-267/bruce-gray/raku/ch-2.raku25
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-267/bruce-gray/raku/ch-1.raku b/challenge-267/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..8be34322d9
--- /dev/null
+++ b/challenge-267/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,38 @@
+# Concise alternatives:
+# my &task1 = *».sign.reduce(&[*]);
+# my &task1 = { [*] @^ns».sign }
+
+# No time to write comparison tests this week; this Bag-based solution also works, with no multiplication.
+# sub task1 ( @ns --> Int ) {
+# my $b = @ns».sign.Bag;
+#
+# return $b{0} ?? 0
+# !! $b{-1} !%% 2 ?? -1
+# !! $b{-1} || $b{1} ?? 1
+# !! Nil;
+# }
+
+# Early return
+sub task1 ( @ns --> Int ) {
+ return Nil if not @ns;
+
+ my $ret = 1;
+
+ for @ns {
+ $ret *= .sign
+ or return 0;
+ }
+ return $ret;
+}
+
+
+use Test; plan +constant @tests =
+ ( 1, ( -1, -2, -3, -4, 3, 2, 1 ) ),
+ ( 0, ( 1, 2, 0, -2, -1 ) ),
+ ( -1, ( -1, -1, 1, -1, 2 ) ),
+
+ ( Nil, [ ] ),
+;
+for @tests -> ( $expected, @ns ) {
+ is task1(@ns), $expected;
+}
diff --git a/challenge-267/bruce-gray/raku/ch-2.raku b/challenge-267/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..878bf9d2d3
--- /dev/null
+++ b/challenge-267/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,25 @@
+sub task2 ( Str $s, UInt $print_limit, @character_widths ) {
+ my %w = 'a'..'z' Z=> @character_widths;
+
+ my @s_char_widths = %w{ $s.comb };
+ die unless @s_char_widths.all ~~ .defined;
+
+ my $line_count = 1;
+ my $line_width = 0;
+
+ for @s_char_widths {
+ $line_width += $_;
+ $line_width = $_, $line_count++ if $line_width > $print_limit;
+ }
+
+ return $line_count, $line_width;
+}
+
+
+use Test; plan +constant @tests =
+ ( (3, 60) , (10 xx 26) , 'abcdefghijklmnopqrstuvwxyz' ),
+ ( (2, 4) , (4,|(10 xx 25)) , 'bbbcccdddaaa' ),
+;
+for @tests -> ( @expected, @widths, $string ) {
+ is-deeply task2($string, 100, @widths), @expected;
+}