From ad5afaa24f905945daec75e9ebcb311d883436f7 Mon Sep 17 00:00:00 2001 From: Simon Proctor Date: Mon, 22 Jul 2019 10:43:34 +0100 Subject: Longest substrings challenge --- challenge-018/simon-proctor/perl6/ch-1.p6 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-018/simon-proctor/perl6/ch-1.p6 diff --git a/challenge-018/simon-proctor/perl6/ch-1.p6 b/challenge-018/simon-proctor/perl6/ch-1.p6 new file mode 100644 index 0000000000..ee828ecfd1 --- /dev/null +++ b/challenge-018/simon-proctor/perl6/ch-1.p6 @@ -0,0 +1,23 @@ +#!/usr/bin/env perl6 + +use v6; + +#| Find the longest common sub strings of a list of strings +sub MAIN ( + *@words where *.elems > 1 #= List of words to compare +) { + my @word-subs = @words.map( &all-substrings ); + .say for ([(&)] @word-subs).keys.sort( { $^b.codes <=> $^a.codes } ).grep( { state $len = $_.codes; $_.codes == $len }); +} + +sub all-substrings( Str $word ) { + my $len = $word.codes; + my @subs; + + for (1..$len) -> $sub-len { + for (0..$len-$sub-len) -> $start { + @subs.push( $word.substr($start,$sub-len) ); + } + } + return set( @subs ); +} -- cgit