aboutsummaryrefslogtreecommitdiff
path: root/challenge-328
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-07-05 20:07:20 +0100
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-07-05 20:07:20 +0100
commita531699beeacf4ea3bc9aec6a30a9ffc6f9d0414 (patch)
treeb8d4d514bed06efd9944c090673f6f4c78e2a4c8 /challenge-328
parentc65be24cab402201d79f2996efd8ecfc2b68eb7e (diff)
downloadperlweeklychallenge-club-a531699beeacf4ea3bc9aec6a30a9ffc6f9d0414.tar.gz
perlweeklychallenge-club-a531699beeacf4ea3bc9aec6a30a9ffc6f9d0414.tar.bz2
perlweeklychallenge-club-a531699beeacf4ea3bc9aec6a30a9ffc6f9d0414.zip
- Added solutions by Mark Anderson.
- Added solutions by Packy Anderson. - Added solutions by Robert Ransbottom.
Diffstat (limited to 'challenge-328')
-rw-r--r--challenge-328/0rir/raku/ch-1.raku87
-rw-r--r--challenge-328/0rir/raku/ch-2.raku60
2 files changed, 147 insertions, 0 deletions
diff --git a/challenge-328/0rir/raku/ch-1.raku b/challenge-328/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..fc78ba89b1
--- /dev/null
+++ b/challenge-328/0rir/raku/ch-1.raku
@@ -0,0 +1,87 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+328-Task 1: Replace all ?
+Submitted by: Mohammad Sajid Anwar
+You are given a string containing only lower case English letters and ?.
+Write a script to replace all ? in the given string so that the string doesn’t contain consecutive repeating characters.
+Example 1
+Input: $str = "a?z"
+Output: "abz"
+There can be many strings, one of them is "abz".
+The choices are 'a' to 'z' but we can't use either 'a' or 'z' to replace the '?'.
+Example 2
+Input: $str = "pe?k"
+Output: "peak"
+Example 3
+Input: $str = "gra?te"
+Output: "grabte"
+=end comment
+
+my @Test =
+ "", "",
+ "?", "a",
+ 'z', "z",
+ "a?", "ab",
+ "?a", "ba",
+ "??", "ab",
+ "??a", "aba",
+ "b??", "bab",
+ "gra?te", "grabte",
+ "g?a?te", "gbabte",
+ "g???te", "gabate",
+ "g????e", "gababe",
+ "g?????", "gababa",
+ 'xy', 'xy',
+ "xyz", "xyz",
+ "azc", "azc",
+ "pe?k", "peak",
+ "??????", "ababab",
+ "g?a?te", "gbabte",
+;
+
+plan +@Test ÷ 2;
+
+# Here we follow a pattern suggested by the examples: replace with the
+# alphabetically first char that fits.
+
+constant \fix-me = '?'; # Char value needing replacement.
+constant \first-choice = 'a'; # First choice for substitutions.
+constant \filler = '!'; # Out of band value for stand-ins.
+
+sub alter( Any:D $q-mark is rw, :$prefix = filler, :$suffix = filler -->Nil) {
+ $q-mark = first-choice;
+ quietly ++$q-mark while $q-mark eq any($prefix, $suffix);
+ return;
+}
+
+multi task( Str:D $a where *.chars == 0 -->Str) { '' }
+multi task( Str:D $a where *.chars == 1 -->Str) {
+ $a eq fix-me ?? first-choice !! $a ;
+}
+multi task( Str:D $a where *.chars ≥ 1 --> Str) {
+ my @s = $a.comb;
+
+ if @s[0] eq fix-me { alter @s[0], :suffix(@s[1]) }
+
+ for 1..(@s.end) -> \i {
+ next if @s[i] ne fix-me;
+ alter @s[i], :prefix(@s[i-1]), :suffix( @s[i+1]);
+ }
+
+ if @s[*.end] eq fix-me { alter @s[*.end], :prefix(@s[*.end-1]) }
+ @s.join;
+}
+
+for @Test -> $in, $exp, {
+ is task( $in), $exp, "{$exp // $exp.^name()} <- $in.raku()";
+}
+
+done-testing;
+
+my $str = "??????a?g?r???a?te?";
+
+say qq{\nInput: \$str = "$str"\nOutput: "}, task($str), '"';
diff --git a/challenge-328/0rir/raku/ch-2.raku b/challenge-328/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..446baf13d2
--- /dev/null
+++ b/challenge-328/0rir/raku/ch-2.raku
@@ -0,0 +1,60 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+328-Task 2: Good String
+Submitted by: Mohammad Sajid Anwar
+You are given a string made up of lower and upper case English letters only.
+Write a script to return the good string of the given string. A string is called good string if it doesn’t have two adjacent same characters, one in upper case and other is lower case.
+UPDATE [2025-07-01]: Just to be explicit, you can only remove pair if they are same characters, one in lower case and other in upper case, order is not important.
+Example 1
+Input: $str = "WeEeekly"
+Output: "Weekly"
+We can remove either, "eE" or "Ee" to make it good.
+Example 2
+Input: $str = "abBAdD"
+Output: ""
+We remove "bB" first: "aAdD"
+Then we remove "aA": "dD"
+Finally remove "dD".
+Example 3
+Input: $str = "abc"
+Output: "abc"
+=end comment
+
+my @Test =
+ # $in $exp
+ "WeEeekly", "Weekly",
+ "abBAdD", "",
+ "abc", "abc",
+ 'abcdefghHGFEDCBa', 'aa',
+ 'bcdefghHGFEDCBa', 'a',
+ 'abcdefghHGFEDCB', 'a',
+ "XxabBAdD", '',
+ "XxnabBAmdD", 'nm',
+ "AaXxnabBAmdDzZ", 'nm',
+;
+plan +@Test ÷ 2;
+
+constant XCASE = 32;
+
+sub task( Str $a is copy --> Str) {
+ my $unchanged;
+ repeat {
+ $unchanged = $a;
+ $a ~~ s:g/ (<:L>)(<:L>) <?{ XCASE == abs($0.ord-$1.ord)}>//;
+ } until $a eq $unchanged;
+ $a;
+}
+
+for @Test -> $in, $exp {
+ is task( $in), $exp, "{$exp // $exp.^name()} <- $in.raku()";
+}
+done-testing;
+
+my $str = "AaaAaBbbbBCcc";
+
+say qq{\nInput: \$str = "$str"\nOutput: "}, task( $str), '"';
+