aboutsummaryrefslogtreecommitdiff
path: root/challenge-256
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-02-26 00:29:48 +0000
committerGitHub <noreply@github.com>2024-02-26 00:29:48 +0000
commit99af58bee2e79adcab66dcee3e3a5643d952b747 (patch)
tree78e5727ad73a4e4367a5fe6b2cfccfca5257143c /challenge-256
parent2ac6c477ca6f1c987edd02f7216cea608fc80aac (diff)
parent7067e03bf4c63c12a55c58d4a8f6de1f098c805e (diff)
downloadperlweeklychallenge-club-99af58bee2e79adcab66dcee3e3a5643d952b747.tar.gz
perlweeklychallenge-club-99af58bee2e79adcab66dcee3e3a5643d952b747.tar.bz2
perlweeklychallenge-club-99af58bee2e79adcab66dcee3e3a5643d952b747.zip
Merge pull request #9647 from 0rir/256
256
Diffstat (limited to 'challenge-256')
-rw-r--r--challenge-256/0rir/raku/ch-1.raku52
-rw-r--r--challenge-256/0rir/raku/ch-2.raku49
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-256/0rir/raku/ch-1.raku b/challenge-256/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..c8465c9e48
--- /dev/null
+++ b/challenge-256/0rir/raku/ch-1.raku
@@ -0,0 +1,52 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+256-1: Maximum Pairs Submitted by: Mohammad Sajid Anwar
+
+Given an array of distinct words, @words, the maximum pairs in the given array. The words $words[i] and $words[j] can be a pair if one is reverse of the other.
+
+Example 1
+Input: @words = ("ab", "de", "ed", "bc")
+Output: 1
+
+Example 2
+Input: @words = ("aa", "ba", "cd", "ed")
+Output: 0
+Example 3
+Input: @words = ("uv", "qp", "st", "vu", "mn", "pq")
+Output: 2
+=end comment
+
+my @Test =
+ 0, (),
+ 0, ("ab",),
+ 0, ("ab","cd"),
+ 1, ("ab", "ba"),
+ 0, ("ab", "bc", "de", "ef"),
+ 1, ("ab", "de", "ed", "bc"),
+ 0, ("aa", "ba", "cd", "ed"),
+ 2, ("uv", "qp", "st", "vu", "mn", "pq"),
+;
+
+plan @Test ÷ 2 + 1;
+
+sub reversed-wordpair-ct( @a -->Int) {
+ die 'not distinct group' unless @a == @a.unique;
+ (@a.map( *.flip) ∩ @a).elems div 2;
+}
+
+for @Test -> $exp, @in {
+ is reversed-wordpair-ct(@in), $exp, "$exp <- @in[]";
+}
+dies-ok { reversed-wordpair-ct( <bad word list bad>) }, "bad word list dies";
+done-testing;
+
+my @word = "uv", "qp", "st", "vu", "mn", "pq";
+say "\nInput: @words = @word.raku()\nOutput: &reversed-wordpair-ct( @word)";
+
+exit;
+
diff --git a/challenge-256/0rir/raku/ch-2.raku b/challenge-256/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..2c85b8978b
--- /dev/null
+++ b/challenge-256/0rir/raku/ch-2.raku
@@ -0,0 +1,49 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+256-2: Merge Strings Submitted by: Mohammad Sajid Anwar
+Given two strings, $str1 and $str2.
+
+Write a script to merge the given strings by adding in alternative order starting with the first string. If a string is longer than the other then append the remaining at the end.
+
+Example 1
+Input: $str1 = "abcd", $str2 = "1234"
+Output: "a1b2c3d4"
+Example 2
+Input: $str1 = "abc", $str2 = "12345"
+Output: "a1b2c345"
+Example 3
+Input: $str1 = "abcde", $str2 = "123"
+Output: "a1b2c3de"
+=end comment
+
+my @Test =
+ # first second ab-result ba-result
+ "a", "b", "ab", "ba",
+ "abcd", "1234", "a1b2c3d4", "1a2b3c4d",
+ "abc", "12345", "a1b2c345", "1a2b3c45",
+ "abcde", "123", "a1b2c3de", "1a2b3cde",
+;
+plan @Test ÷ 2;
+
+sub interleave( Str $a where ?*.chars, Str $b where ?*.chars -->Str) {
+ roundrobin($a.comb, $b.comb).flat.join;
+}
+
+for @Test -> $a, $b, $ab-exp, $ba-exp {
+ is interleave($a, $b), $ab-exp, "$ab-exp <- $a interleave $b";
+ is interleave($b, $a), $ba-exp, "$ba-exp <- $b interleave $a";
+}
+
+done-testing;
+my $a = "123456789";
+my $b = "abc";
+
+say "\nInput: \$str1 = $a, \$str2 = $b\nOutput: &interleave($a,$b)";
+
+exit;
+