aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-05 21:32:02 +0100
committerGitHub <noreply@github.com>2024-05-05 21:32:02 +0100
commit38731f7c6d87e585e3bacdee36aa22cc76ea8642 (patch)
tree98113056157b73dfdcd7999dfe57ea774b856d78
parent01bbda5685a45abbd5ed652e67f95d206783d0c9 (diff)
parente3e9d4e9fc905e73107232cd9c53e946810bbc71 (diff)
downloadperlweeklychallenge-club-38731f7c6d87e585e3bacdee36aa22cc76ea8642.tar.gz
perlweeklychallenge-club-38731f7c6d87e585e3bacdee36aa22cc76ea8642.tar.bz2
perlweeklychallenge-club-38731f7c6d87e585e3bacdee36aa22cc76ea8642.zip
Merge pull request #10040 from 0rir/work
267
-rw-r--r--challenge-267/0rir/raku/ch-1.raku71
-rw-r--r--challenge-267/0rir/raku/ch-2.raku154
2 files changed, 225 insertions, 0 deletions
diff --git a/challenge-267/0rir/raku/ch-1.raku b/challenge-267/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..dc8ad02c04
--- /dev/null
+++ b/challenge-267/0rir/raku/ch-1.raku
@@ -0,0 +1,71 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+267-1: Product Sign Submitted by: Mohammad Sajid Anwar
+You are given an array of @ints.
+
+Write a script to find the sign of product of all integers in the given
+array. The sign is 1 if the product is positive, -1 if the product is
+negative and 0 if product is zero.
+
+Example 1
+Input: @ints = (-1, -2, -3, -4, 3, 2, 1)
+Output: 1
+
+The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0
+Example 2
+Input: @ints = (1, 2, 0, -2, -1)
+Output: 0
+
+The product 1 x 2 x 0 x -2 x -1 => 0
+Example 3
+Input: @ints = (-1, -1, 1, -1, 2)
+Output: -1
+
+The product -1 x -1 x 1 x -1 x 2 => -2 < 0
+
+=end comment
+
+my @Test =
+ Int, (),
+ Int, ( 0,),
+ Int, (-1,),
+ Int, ( 1,),
+ -1, ( 1, -1),
+ 1, ( 1, 1),
+ 0, ( 0, 0),
+ 0, ( 1, 0),
+ 0, (-1, 0),
+ 1, (-1, -2, -3, -4, 3, 2, 1),
+ 0, ( 1, 2, 0, -2, -1),
+ -1, (-1, -1, 1, -1, 2),
+ 1, ( flat -1 xx 10_000, 1 xx 9_999 ).List,
+ -1, ( flat -1 xx 9_999, 1 xx 9_999 ).List,
+ 0, ( flat 0, -1 xx 100_000, 1 xx 99_999 ).List,
+ 0, ( flat -1 xx 100_000, 1 xx 99_999, 0 ).List,
+;
+
+plan @Test ÷ 2;
+
+sub mult-sign( List $a) {
+ return Int if $a ~~ Empty or +@$a == 1;
+ my ( $n, $z, $p) = hyper $a».sign.Bag{-1, 0, 1}; # Thanks Util
+ return 0 if $z;
+ return -1 unless $n %% 2;
+ return 1;
+}
+
+for @Test -> $exp, $in {
+ is mult-sign($in), $exp, "$exp.raku() <- $in.gist()";
+}
+
+done-testing;
+my @int = flat -1 xx 11,1 xx 10;
+
+say "\nInput: @int = @int[]\nOutput: &mult-sign(@int)";
+
+exit;
+
diff --git a/challenge-267/0rir/raku/ch-2.raku b/challenge-267/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..7f0b12e402
--- /dev/null
+++ b/challenge-267/0rir/raku/ch-2.raku
@@ -0,0 +1,154 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+267-2: Line Counts Submitted by: Mohammad Sajid Anwar
+You are given a string, $str, and a 26-items array @widths containing
+the width of each character from a to z.
+
+
+Write a script to find out the number of lines and the width of the
+last line needed to display the given string, assuming you can only fit
+100 width units on a line.
+
+Example 1
+Input: $str = "abcdefghijklmnopqrstuvwxyz"
+ @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)
+Output: (3, 60)
+
+Line 1: abcdefghij (100 pixels)
+Line 2: klmnopqrst (100 pixels)
+Line 3: uvwxyz (60 pixels)
+Example 2
+Input: $str = "bbbcccdddaaa"
+ @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)
+Output: (2, 4)
+
+Line 1: bbbcccdddaa (98 pixels)
+Line 2: a (4 pixels)
+
+=end comment
+
+
+my @Test =
+ #text char-width expect max-pixels-per-line
+ "", ( 10 x 25, 1), ('',0), 100,
+
+ "abcdefghij", ( 10 xx 26), (1, 100), 100,
+
+ "abcde", ( 10 xx 26), (1, 50), 100,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ (10,10,10,10,10,10,10,10,10,10,10,10,10,
+ 10,10,10,10,10,10,10,10,10,10,10,10,10),
+ (3, 60),
+ 100,
+
+ "bbbcccdddaaa",
+ ( 4,10,10,10,10,10,10,10,10,10,10,10,10,
+ 10,10,10,10,10,10,10,10,10,10,10,10,10),
+ (2, 4),
+ 100,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ ( 4,100,10,10,10,10,10,10,10,10,10,10,10,
+ 10,10,10,10,10,10,10,10,10,10,10,10,10),
+ (5, 40),
+ 100,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ (flat 5 xx 11, 3, 7, 5 xx 9, 7,5,5,5 ),
+ (2, 32),
+ 100,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ (flat 5 xx 11, 3, 7, 5 xx 9, 7,5,5,5 ),
+ (3, 32),
+ 50,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ (flat 5 xx 11, 3, 9, 5 xx 9, 9,5,5,5 ),
+ (3, 39),
+ 50,
+;
+
+my @Die =
+ "abcdefghijklmnopqrstuvwxyz",
+ (flat 101, 5 xx 25),
+ (3, 60),
+ 100,
+
+ "abcdefghijklmnopqrstuvwxyz",
+ (flat 5 xx 11, 3, 7, 5 xx 9, 7,5,5,101 ),
+ (3, 60),
+ 100,
+;
+
+plan @Test ÷ 4 + @Die ÷ 4;
+
+constant @abc = 'a'..'z';
+my $line-size = 100;
+
+multi pixel-lines-w-tail-size( '', @char-width ) { ('', 0) }
+multi pixel-lines-w-tail-size( Str:D $s, @char-width -->List) {
+
+ # create array of widths for chars
+ # my @width = do { $s.comb.List.map: { %(@abc Z=> @char-width){ $_}}};
+
+ # create array of IntStr( width, pixels) just for error message
+ my %intstr = %( @abc Z=> @char-width.map( {IntStr.new( $_, "$_")}));
+ my @width = $s.comb.List.map( { %intstr{$_} });
+
+ my $lino = 1; # line number accumulator
+ my $cur = 0; # current index into @width
+ my $pixels = 0; # pixel accumulater found in a line
+
+ while $cur < @width {
+
+ die "char '@width[$cur]' exceeds line width: loop $cur"
+ if @width[$cur] > $line-size;
+
+ $pixels += @width[$cur];
+
+ given $pixels <=> $line-size {
+ when Less { ++$cur; }
+ when More {
+ if @width[$cur] == $line-size {
+ $pixels = 0;
+ } else {
+ $pixels = @width[$cur];
+ ++$cur;
+ }
+ ++$lino;
+ }
+ when Same {
+ last if $cur == @width.end;
+ ++$lino;
+ $pixels = 0;
+ ++$cur;
+ }
+ }
+ }
+ return ( $lino, $pixels);
+}
+
+for @Test -> $in, @width, @exp, $ls {
+ $line-size = $ls;
+ is pixel-lines-w-tail-size($in, @width), @exp, "@exp.raku() <- $in";
+ }
+for @Die -> $in, @width, @exp, $ls {
+ $line-size = $ls;
+ dies-ok { pixel-lines-w-tail-size($in, @width)},
+ "DIES: a char wider than line";
+}
+
+done-testing;
+
+$line-size = 100;
+my $str = "abcdefghijklmnopqrstuvwxyz";
+my @width = (flat 5 xx 11, 3, 7, 5 xx 9, 7,5,5,5 );
+
+say qq{\nInput: \$str = "$str"\n @width = @width.raku()\nOutput: },
+ pixel-lines-w-tail-size($str, @width);