diff options
| -rw-r--r-- | challenge-018/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-018/jaldhar-h-vyas/perl5/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-018/jaldhar-h-vyas/perl5/ch-2.pl | 10 | ||||
| -rwxr-xr-x | challenge-018/jaldhar-h-vyas/perl6/ch-1.p6 | 19 | ||||
| -rwxr-xr-x | challenge-018/jaldhar-h-vyas/perl6/ch-2.p6 | 2 |
5 files changed, 52 insertions, 9 deletions
diff --git a/challenge-018/jaldhar-h-vyas/blog.txt b/challenge-018/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..cfa3df0edd --- /dev/null +++ b/challenge-018/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2019/08/perl_weekly_challenge_week_18.html
\ No newline at end of file diff --git a/challenge-018/jaldhar-h-vyas/perl5/ch-1.pl b/challenge-018/jaldhar-h-vyas/perl5/ch-1.pl new file mode 100755 index 0000000000..65b9b72876 --- /dev/null +++ b/challenge-018/jaldhar-h-vyas/perl5/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/perl +use warnings; +use strict; +use 5.010; + +sub LongestCommonSubstring { + my @strings = sort { length $a <=> length $b } @_; + + my $shortest = shift @strings; + my @suffixes; + for my $i (0 .. length($shortest) - 1) { + for my $j ($i .. length($shortest) - 1) { + push @suffixes, substr $shortest, $i, $j - $i + 1; + } + } + @suffixes = reverse sort { length $a <=> length $b } @suffixes; + + SUFFIX: for my $suffix (@suffixes) { + for my $string (@strings) { + if ( $string !~ /$suffix/) { + next SUFFIX; + } + } + return $suffix; + } +} + +say LongestCommonSubstring('ABABC', 'BABCA', 'ABCBA'); + diff --git a/challenge-018/jaldhar-h-vyas/perl5/ch-2.pl b/challenge-018/jaldhar-h-vyas/perl5/ch-2.pl index 826279354d..f8f01c544e 100755 --- a/challenge-018/jaldhar-h-vyas/perl5/ch-2.pl +++ b/challenge-018/jaldhar-h-vyas/perl5/ch-2.pl @@ -44,14 +44,9 @@ data structure where the items are sorted in order of numeric priority. =cut -has _iterator => ( - is => 'rw', - default => 0, -); - has _queue => ( is => 'rw', - default => sub { [] } # should use a binary heap really but its all good... + default => sub { [] } ); has _size => ( @@ -63,7 +58,7 @@ has _size => ( =head3 clear() - removes all elements from the priority queue and resets iteration. + removes all elements from the priority queue. =cut @@ -72,7 +67,6 @@ sub clear { $self->_queue([]); $self->_size(0); - $self->_iterator(0); } =head3 top() diff --git a/challenge-018/jaldhar-h-vyas/perl6/ch-1.p6 b/challenge-018/jaldhar-h-vyas/perl6/ch-1.p6 new file mode 100755 index 0000000000..a8780681f5 --- /dev/null +++ b/challenge-018/jaldhar-h-vyas/perl6/ch-1.p6 @@ -0,0 +1,19 @@ +#!/usr/bin/perl6 + +sub LongestCommonSubstring(*@strings) { + @strings = @strings.sort(*.chars); + + my $shortest = @strings.shift; + my @substrings; + for 0 ..^ $shortest.chars -> $i { + for $i ..^ $shortest.chars -> $j { + @substrings.push($shortest.substr($i, $j - $i + 1)); + } + } + + return @substrings.sort(-*.chars) + .grep({ @strings.all.contains($_) }) + .first; +} + +say LongestCommonSubstring('ABABC', 'BABCA', 'ABCBA'); diff --git a/challenge-018/jaldhar-h-vyas/perl6/ch-2.p6 b/challenge-018/jaldhar-h-vyas/perl6/ch-2.p6 index 693066c50f..91718af12e 100755 --- a/challenge-018/jaldhar-h-vyas/perl6/ch-2.p6 +++ b/challenge-018/jaldhar-h-vyas/perl6/ch-2.p6 @@ -9,7 +9,7 @@ class Data::PriorityQueue { has Element @!queue = (); - method clear { + method clear() { @!queue = (); } |
