aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-12-28 20:34:27 +0000
committerGitHub <noreply@github.com>2023-12-28 20:34:27 +0000
commitabe9ad1dbf3fde9f35a793da2b2c634c494d1d1f (patch)
tree7cc4afe0279af3596768289b071792e7fe6468bf
parent3a888e3f8140a2e0043c7f287700deecd1fdf5a1 (diff)
parent3055361e11c6f6f50171a56e905af089d17da237 (diff)
downloadperlweeklychallenge-club-abe9ad1dbf3fde9f35a793da2b2c634c494d1d1f.tar.gz
perlweeklychallenge-club-abe9ad1dbf3fde9f35a793da2b2c634c494d1d1f.tar.bz2
perlweeklychallenge-club-abe9ad1dbf3fde9f35a793da2b2c634c494d1d1f.zip
Merge pull request #9308 from 0rir/249
249
-rw-r--r--challenge-249/0rir/raku/ch-1.raku60
-rw-r--r--challenge-249/0rir/raku/ch-2.raku51
2 files changed, 111 insertions, 0 deletions
diff --git a/challenge-249/0rir/raku/ch-1.raku b/challenge-249/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..41daf052f1
--- /dev/null
+++ b/challenge-249/0rir/raku/ch-1.raku
@@ -0,0 +1,60 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+249-1: Equal Pairs Submitted by: Mohammad S Anwar
+You are given an array of integers with even number of elements.
+Write a script to divide the given array into equal pairs such that:
+a) Each element belongs to exactly one pair.
+b) The elements present in a pair are equal.
+
+Example 1
+Input: @ints = (3, 2, 3, 2, 2, 2)
+Output: (2, 2), (3, 3), (2, 2)
+
+Example 2
+Input: @ints = (1, 2, 3, 4)
+Output: ()
+=end comment
+
+my @Test =
+ (3,2,3,2,2,2), ((2,2), (3,3), (2,2)),
+ (1,2,3,4), (),
+ (1,1,2,2,2,2,3,3,3,3,3,3,4,4),
+ ((1,1), (2,2), (2,2), (3,3),(3,3),(3,3),(4,4)),
+;
+my @Triplet =
+ (3, 2, 3, 2, 2, 2), (),
+ (3, 3, 3, 2, 2, 2), ((2, 2, 2), (3, 3, 3)),
+ (1, 2, 3, 4), (),
+ (1,1,1), (1,1,1),
+;
+plan @Test ÷ 2 + @Triplet ÷ 2;
+
+sub equal-sets( $a is copy, Int $size = 2 -->List) {
+ my $ret;
+ $a.=BagHash;
+ for @$a -> $p {
+ return () unless (my $set-ct = $p.value ÷ $size) == $set-ct.floor;
+ $ret.append: ($p.key xx $size) xx $set-ct;
+ }
+ $ret.List;
+}
+
+for @Test -> $in, $exp {
+ is equal-sets($in).sort( *[0]), $exp.sort( *[0]),
+ "Sorted Pairs $exp.sort().List.raku() < $in.raku()";
+}
+for @Triplet -> $in, $exp {
+ is equal-sets($in,3).sort, $exp.sort,
+ "Sorted Triplets $exp.sort().List.raku() < $in.raku()";
+}
+done-testing;
+
+my @int = (1,1,2,2,2,2,3,3,3,3,3,3,4,4);
+say "\nInput: @int = (@int[])\nOutput: &equal-sets( @int)<>.raku()";
+
+exit;
+
diff --git a/challenge-249/0rir/raku/ch-2.raku b/challenge-249/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..25cdb0e6ff
--- /dev/null
+++ b/challenge-249/0rir/raku/ch-2.raku
@@ -0,0 +1,51 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+249-2: DI String Match Submitted by: Mohammad S Anwar
+You are given a string s, consisting of only the characters "D" and "I".
+Find a permutation of the integers [0 .. length(s)] such that for each character s[i] in the string:
+
+s[i] == 'I' ⇒ perm[i] < perm[i + 1]
+s[i] == 'D' ⇒ perm[i] > perm[i + 1]
+
+Example 1
+Input: $str = "IDID"
+Output: (0, 4, 1, 3, 2)
+Example 2
+Input: $str = "III"
+Output: (0, 1, 2, 3)
+Example 3
+Input: $str = "DDI"
+Output: (3, 2, 0, 1)
+=end comment
+
+my @Test =
+ "IDID", (0, 4, 1, 3, 2),
+ "III", (0, 1, 2, 3),
+ "DDI", (3, 2, 0, 1),
+ "I", (0,1),
+ "D", (1,0),
+ "DDDDDDDIIIIIIIIII", (17,16,15,14,13,12,11,0,1,2,3,4,5,6,7,8,9,10,),
+;
+plan @Test ÷ 2;
+
+sub i-did( Any:D(Str) $a where *.chars > 0, $low-char = 'I' --> List) {
+ my @c = $a.comb;
+ my ($i, $d) = 0, @c.elems;
+ @c.=map( { $_ ~~ $low-char ?? $i++ !! $d-- } );
+ @c.push: $_ ~~ $low-char ?? $i !! $d ;
+ @c.List;
+}
+
+for @Test -> $in, @exp {
+ is i-did($in), @exp, "$in -> @exp.raku()";
+}
+done-testing;
+
+my $str = "DDDDDDDIIIIIIIIII";
+say "\nInput: \$str = $str\nOutput: &i-did( $str)";
+exit;
+