aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-02-15 04:55:14 +0000
committerGitHub <noreply@github.com>2021-02-15 04:55:14 +0000
commitde8c1e5d220f3835b40d8d89ca1b89ee75c8cfd2 (patch)
tree867bd24b1d4d9b0b1a04b4eb37becafc08c85156
parent3a9016c6948dadeedd4b84ae8c53d1f23dca7a51 (diff)
parent2a385c0a609fd562db39df726ebe9319a302742f (diff)
downloadperlweeklychallenge-club-de8c1e5d220f3835b40d8d89ca1b89ee75c8cfd2.tar.gz
perlweeklychallenge-club-de8c1e5d220f3835b40d8d89ca1b89ee75c8cfd2.tar.bz2
perlweeklychallenge-club-de8c1e5d220f3835b40d8d89ca1b89ee75c8cfd2.zip
Merge pull request #3523 from pkmnx/branch-for-challenge-99
no. 2, for 99
-rwxr-xr-xchallenge-099/pkmnx/raku/ch-2.raku45
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-099/pkmnx/raku/ch-2.raku b/challenge-099/pkmnx/raku/ch-2.raku
new file mode 100755
index 0000000000..17c80f42fe
--- /dev/null
+++ b/challenge-099/pkmnx/raku/ch-2.raku
@@ -0,0 +1,45 @@
+#!/usr/bin/env raku
+
+my $S = "littleit";
+my $T = "lit";
+
+#my $S = "london";
+#my $T = "lon";
+
+my $outp = find( $S, $T );
+
+printf( "Input: \$S = \"%s' \$T = '%s'\n", $S, $T );
+printf( "Output: %d\n\n", $outp.elems );
+
+for (^$outp.elems) {
+ printf(" %4s: %s\n", $_ +1, $outp[$_] );
+}
+
+sub find( $S, $T ) {
+ my $outr = [];
+ unqsubsq( $S, 0, $T, 0, "", $outr );
+ return $outr;
+}
+
+sub unqsubsq( $inp, $i, $mt, $j, $outp, $outr ) {
+ if ( $i > $inp.chars && $j >= $mt.chars ) {
+ $outr.prepend($outp);
+ return;
+ }
+ return if $i > $inp.chars;
+ my ( $iv, $jv, $noutp ) = ( $inp.substr($i,1), $mt.substr($j,1), $outp );
+ if ( $iv.chars > 0 and $iv ~~ $jv ) {
+ unqsubsq( $S, $i+1, $T, $j, $noutp~$iv, $outr );
+ my $nc = "[$iv]";
+ if ( $outp.chars > 0 ) {
+ my $lastChar = ($outp.comb)[$outp.chars -1];
+ if ( $lastChar ~~ "]" ) {
+ $noutp = $noutp.subst( /\]$/, "" );
+ $nc = "$iv]";
+ }
+ }
+ unqsubsq( $S, $i+1, $T, $j+1, $noutp~$nc, $outr );
+ } else {
+ unqsubsq( $S, $i+1, $T, $j, $noutp~$iv, $outr );
+ }
+}