aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Hood <hood@panix.com>2021-02-14 23:01:40 -0500
committerPhilip Hood <hood@panix.com>2021-02-14 23:01:40 -0500
commit2a385c0a609fd562db39df726ebe9319a302742f (patch)
tree867bd24b1d4d9b0b1a04b4eb37becafc08c85156
parent3a9016c6948dadeedd4b84ae8c53d1f23dca7a51 (diff)
downloadperlweeklychallenge-club-2a385c0a609fd562db39df726ebe9319a302742f.tar.gz
perlweeklychallenge-club-2a385c0a609fd562db39df726ebe9319a302742f.tar.bz2
perlweeklychallenge-club-2a385c0a609fd562db39df726ebe9319a302742f.zip
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 );
+ }
+}