aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2024-03-30 08:44:18 -0400
committerrir <rirans@comcast.net>2024-03-30 08:44:18 -0400
commit2161ff780ed932a65f07ba9ea9dacbd502f693d8 (patch)
treea186706b22e03abaaf9f611bde65ce09826e9926
parent878c41e579ab40444980e4f3b02c7693f751ac0e (diff)
downloadperlweeklychallenge-club-2161ff780ed932a65f07ba9ea9dacbd502f693d8.tar.gz
perlweeklychallenge-club-2161ff780ed932a65f07ba9ea9dacbd502f693d8.tar.bz2
perlweeklychallenge-club-2161ff780ed932a65f07ba9ea9dacbd502f693d8.zip
262
-rw-r--r--challenge-262/0rir/raku/ch-1.raku65
-rw-r--r--challenge-262/0rir/raku/ch-2.raku63
2 files changed, 128 insertions, 0 deletions
diff --git a/challenge-262/0rir/raku/ch-1.raku b/challenge-262/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..463831657a
--- /dev/null
+++ b/challenge-262/0rir/raku/ch-1.raku
@@ -0,0 +1,65 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6d;
+use Test;
+
+=begin comment
+262-1: Max Positive Negative Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints.
+
+Write a script to return the maximum number of either positive or negative integers in the given array.
+
+Example 1
+Input: @ints = (-3, 1, 2, -1, 3, -2, 4)
+Output: 4
+
+Count of positive integers: 4
+Count of negative integers: 3
+Maximum of count of positive and negative integers: 4
+Example 2
+Input: @ints = (-1, -2, -3, 1)
+Output: 3
+
+Count of positive integers: 1
+Count of negative integers: 3
+Maximum of count of positive and negative integers: 3
+Example 3
+
+Count of positive integers: 2
+Count of negative integers: 0
+Maximum of count of positive and negative integers: 2
+=end comment
+
+my @Test =
+ (-3, 1, 2, -1, 3, -2, 4), 4,
+ (-1, -2, -3, 1), 3,
+ (1,2), 2,
+ (-2,2), 1,
+ (-2,), 1,
+ (2,), 1,
+ (1,0,1), 2,
+ (-1,0,0,0,1), 1,
+ (1,0,0,0,1), 2,
+ (0,0,0,0,0), 0,
+ (), 0,
+
+;
+plan @Test ÷ 2;
+
+sub func( @a -->Int) {
+ my %h = @a.classify: { when $_ < 0 { Less }
+ when $_ > 0 { More } }
+
+ for Less, More { %h{$_} = %h{$_} :exists ?? %h{$_}.elems !! 0 }
+
+ %h{Less} max %h{More}
+}
+
+for @Test -> @in, $exp {
+ is func(@in), $exp, "$exp <- @in.sort()";
+}
+done-testing;
+
+my @in = -3, 1, 2, -1, 3, -2, 4;
+say "\nInput: @in = @in.raku()\nOutput: &func(@in)"
+
diff --git a/challenge-262/0rir/raku/ch-2.raku b/challenge-262/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..b3c6d454c4
--- /dev/null
+++ b/challenge-262/0rir/raku/ch-2.raku
@@ -0,0 +1,63 @@
+#!/usr/bin/env rak;
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ;
+use v6;
+INIT $*RAT-OVERFLOW = FatRat;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+
+262-2: Count Equal Divisible
+Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @ints and an integer $k.
+
+Write a script to return the number of pairs (i, j) where
+
+a) 0 <= i < j < size of @int
+b) ints[i] == ints[j
+c) i x j is divisible by k
+Example 1
+Input: @ints = (3,1,2,2,2,1,3) and $k = 2
+Output: 4
+
+(0, 6) => ints[0] == ints[6] and 0 x 6 is divisible by 2
+(2, 3) => ints[2] == ints[3] and 2 x 3 is divisible by 2
+(2, 4) => ints[2] == ints[4] and 2 x 4 is divisible by 2
+(3, 4) => ints[3] == ints[4] and 3 x 4 is divisible by 2
+Example 2
+Input: @ints = (1,2,3) and $k = 2
+Output: 0
+=end comment
+
+my @Test =
+ # $exp @in $k
+ 4, (3,1,2,2,2,1,3), 2,
+ 0, (1,2,3), 1,
+ 0, (), 1,
+ 0, (1,), 0,
+ 3, (0,0,0,), 1,
+;
+plan @Test ÷ 3;
+
+multi func( Int $k, @in where *.end == -1 ) { 0 }
+multi func( Int:D $k, @in) {
+ # a) 0 <= i < j < size of @ints
+ #b) ints[i] == ints[j]
+ #c) i x j is divisible by k
+ (^@in).combinations(2).grep( {
+ ( .[0] < .[1] )
+ and ( .[0] × .[1] %% $k )
+ and ( @in[ $_[0]] == @in[ $_[1]] )
+ }).Int;
+}
+
+for @Test -> $exp, @in, $k {
+ is func($k, @in), $exp, "$exp <- $k «- @in.raku()";
+}
+
+done-testing;
+my @in = ( 1,1,1,1,1,3,3,3);
+my $j = 3;
+say "Input: @ints = @in[] and \$k = $j\n Output: &func($j, @in)"
+
+