aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2020-04-23 09:25:40 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2020-04-23 09:25:40 +0100
commit06c3c20933fdc968709f8443bcf1e759c39bcb80 (patch)
treeb9c1ba81297225fc5a9e605f8dec71ea75c6f5fe
parentd7e5920a35377fa1d47fdf737bdb0a2e9418a2d1 (diff)
downloadperlweeklychallenge-club-06c3c20933fdc968709f8443bcf1e759c39bcb80.tar.gz
perlweeklychallenge-club-06c3c20933fdc968709f8443bcf1e759c39bcb80.tar.bz2
perlweeklychallenge-club-06c3c20933fdc968709f8443bcf1e759c39bcb80.zip
- Added Raku solutions to Shortest Unique Prefix task.
-rw-r--r--challenge-057/mohammad-anwar/raku/ch-2.p627
-rw-r--r--challenge-057/mohammad-anwar/raku/ch-2a.p632
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-057/mohammad-anwar/raku/ch-2.p6 b/challenge-057/mohammad-anwar/raku/ch-2.p6
new file mode 100644
index 0000000000..96d5405f76
--- /dev/null
+++ b/challenge-057/mohammad-anwar/raku/ch-2.p6
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl6
+
+use v6.d;
+
+sub shortest-unique-prefix($words where .all ~~ Str) {
+ my $p = [];
+ for |$words -> $word {
+ my $i = 1;
+ my $l = $word.chars;
+ while $i < $l {
+ my $char = $word.substr(0, $i);
+ my $count = $words.grep({ m/^$char/ }).elems;
+
+ $count > 1 && $i++ and next;
+ $p.push: $char and last;
+ }
+ }
+
+ return $p;
+}
+
+unit sub MAIN();
+
+my $words = [ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ];
+my $unique = shortest-unique-prefix($words);
+
+say sprintf("[ %s ]", $unique.join(", "));
diff --git a/challenge-057/mohammad-anwar/raku/ch-2a.p6 b/challenge-057/mohammad-anwar/raku/ch-2a.p6
new file mode 100644
index 0000000000..8d39ce40b7
--- /dev/null
+++ b/challenge-057/mohammad-anwar/raku/ch-2a.p6
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl6
+
+use Test;
+
+my $unit-tests = [
+ { in => [ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ],
+ out => [ "alph", "b", "car", "cadm", "cade", "alpi" ]
+ }
+];
+
+for $unit-tests -> (:@in, :@out) {
+ is-deeply(shortest-unique-prefix(@in), @out);
+}
+
+done-testing;
+
+sub shortest-unique-prefix($words where .all ~~ Str) {
+ my $p = [];
+ for |$words -> $word {
+ my $i = 1;
+ my $l = $word.chars;
+ while $i < $l {
+ my $char = $word.substr(0, $i);
+ my $count = $words.grep({ m/^$char/ }).elems;
+
+ $count > 1 && $i++ and next;
+ $p.push: $char and last;
+ }
+ }
+
+ return $p;
+}