aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander <39702500+threadless-screw@users.noreply.github.com>2019-07-24 06:35:57 +0000
committerGitHub <noreply@github.com>2019-07-24 06:35:57 +0000
commitcd4d6cdee8376715f0f48252e42f6c16fd73a3bc (patch)
tree2ef341631eaf21b71088c4935781e5e53dc1efaf
parent0bcaecb707c2f9ebe12a1666d616b896ac5a11dd (diff)
downloadperlweeklychallenge-club-cd4d6cdee8376715f0f48252e42f6c16fd73a3bc.tar.gz
perlweeklychallenge-club-cd4d6cdee8376715f0f48252e42f6c16fd73a3bc.tar.bz2
perlweeklychallenge-club-cd4d6cdee8376715f0f48252e42f6c16fd73a3bc.zip
Create ch-2b.p6
-rw-r--r--challenge-018/ozzy/perl6/ch-2b.p625
1 files changed, 25 insertions, 0 deletions
diff --git a/challenge-018/ozzy/perl6/ch-2b.p6 b/challenge-018/ozzy/perl6/ch-2b.p6
new file mode 100644
index 0000000000..45fa31e654
--- /dev/null
+++ b/challenge-018/ozzy/perl6/ch-2b.p6
@@ -0,0 +1,25 @@
+#!/usr/bin/env perl6
+# Find longest common substring(s) for 2+ cmdline specified strings.
+# Set+sort solution inspired by Rosetta code solution.
+
+sub MAIN (*@strings) {
+
+ if @strings.elems < 2 { say "Usage: cmd <string1> <string2> [stringx]..."; exit 1 }
+
+ my @substrings;
+ for 0..@strings.elems-1 -> $i { # $i = string index into @strings
+ my $l = @strings[$i].chars; # $l = string length
+ for 0..$l-1 -> $j { # $j = start index into string
+ for $j..$l-1 -> $k { # $k = end index of string
+ @substrings[$i].push: @strings[$i].substr($j, ($k-$j+1)); # Generate all substrings from string
+ } #+ in form of 2D-array; 1st dim. is string
+ } #+ index, 2nd dim. is substring index.
+ }
+ my @cs = ([∩] @substrings).keys.sort({$^b.chars <=> $^a.chars}); # Length-sorted intersection of substrings
+ if @cs.elems != 0 {
+ say "List of longest common substrings: "; # Output lcs if we found any.
+ my $i=0; while @cs[$i].chars == @cs[0].chars { say @cs[$i++] };
+ } else {
+ say "No common substring found";
+ }
+}