aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander <39702500+threadless-screw@users.noreply.github.com>2019-07-24 06:51:54 +0000
committerGitHub <noreply@github.com>2019-07-24 06:51:54 +0000
commitf6563268fa6dd71826042e829c1b5d6b1b807794 (patch)
tree7320f7f61342cb04a1a2d21964ba228b8c76442e
parent0bcaecb707c2f9ebe12a1666d616b896ac5a11dd (diff)
downloadperlweeklychallenge-club-f6563268fa6dd71826042e829c1b5d6b1b807794.tar.gz
perlweeklychallenge-club-f6563268fa6dd71826042e829c1b5d6b1b807794.tar.bz2
perlweeklychallenge-club-f6563268fa6dd71826042e829c1b5d6b1b807794.zip
Create ch-2a.p6
-rw-r--r--perl6/ch-2a.p620
1 files changed, 20 insertions, 0 deletions
diff --git a/perl6/ch-2a.p6 b/perl6/ch-2a.p6
new file mode 100644
index 0000000000..92f61119e1
--- /dev/null
+++ b/perl6/ch-2a.p6
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl6
+# Find single longest common substring for just 2 cmdline specified strings.
+# This was a first attempt; the low-level approach seems somewhat difficult to generalize to multiple lcs in 2+ strings.
+
+sub MAIN (Str $string1 , Str $string2) {
+
+ my @string1 = $string1.comb;
+ my @string2 = $string2.comb;
+ my ($l1, $l2) = (@string1.elems, @string2.elems);
+ my ($x,$z) = (0,0); # $x is lcs-index into @string1, $z is length of lcs
+
+ for 0..$l1-1 -> $i {
+ for 0..$l2-1 -> $j {
+ my $k=0;
+ $k++ while ($i+$k < $l1) && ($j+$k < $l2) && (@string1[$i+$k] eq @string2[$j+$k]);
+ if $k > $z { ($x, $z) = ($i, $k) };
+ }
+ }
+ say "LCS = { $string1.substr($x, $z) }";
+}