aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2020-06-15 11:15:05 +0200
committerLuca Ferrari <fluca1978@gmail.com>2020-06-15 11:15:17 +0200
commit19e6acc3823b40bf860a3cdf05f47613ea7ed311 (patch)
tree4f725874a5dfe3bca77e3529a4877e4fa5e0456c
parent7aabb5c6c9021adb0510e653d7deffb72f6e13af (diff)
downloadperlweeklychallenge-club-19e6acc3823b40bf860a3cdf05f47613ea7ed311.tar.gz
perlweeklychallenge-club-19e6acc3823b40bf860a3cdf05f47613ea7ed311.tar.bz2
perlweeklychallenge-club-19e6acc3823b40bf860a3cdf05f47613ea7ed311.zip
Task 2 done.
-rw-r--r--challenge-065/luca-ferrari/raku/ch-1.p62
-rw-r--r--challenge-065/luca-ferrari/raku/ch-2.p632
2 files changed, 33 insertions, 1 deletions
diff --git a/challenge-065/luca-ferrari/raku/ch-1.p6 b/challenge-065/luca-ferrari/raku/ch-1.p6
index c1337e9fbd..15d37d9766 100644
--- a/challenge-065/luca-ferrari/raku/ch-1.p6
+++ b/challenge-065/luca-ferrari/raku/ch-1.p6
@@ -16,7 +16,7 @@
sub MAIN( Int $N where { $N > 0 },
- Int $S where { $S > 0 } ) {
+ Int $S where { $S >= 1 && $S <= 9 } ) {
my @found;
diff --git a/challenge-065/luca-ferrari/raku/ch-2.p6 b/challenge-065/luca-ferrari/raku/ch-2.p6
new file mode 100644
index 0000000000..a30cca8246
--- /dev/null
+++ b/challenge-065/luca-ferrari/raku/ch-2.p6
@@ -0,0 +1,32 @@
+#!raku
+
+# You are given a string $S. Write a script print all possible partitions that gives Palindrome.
+# Return -1 if none found.
+# Please make sure, partition should not overlap.
+# For example, for given string “abaab”,
+# the partition “aba” and “baab” would not be valid, since they overlap.
+
+
+sub MAIN( Str $S where { $S.chars > 0 } = "abaab" ){
+ "Searching non overlapping palindrome partitions on $S".say;
+ my @palindromes;
+
+ my $last = -1;
+ for 0 ..^ $S.chars -> $start {
+ # avoid overlapping
+ next if $start <= $last;
+
+ for $start + 1 ..^ $S.chars -> $end {
+ my $string = $S.split( '', :skip-empty )[ $start .. $end ];
+ if $string ~~ $string.reverse {
+ @palindromes.push: $string;
+ # avoid overlapping
+ $last = $end;
+ last;
+ }
+
+ }
+ }
+
+ @palindromes ?? @palindromes.join( ',' ).say !! "-1";
+}