aboutsummaryrefslogtreecommitdiff
path: root/challenge-018
diff options
context:
space:
mode:
authorJoelle Maslak <jmaslak@antelope.net>2019-07-27 18:25:26 -0600
committerJoelle Maslak <jmaslak@antelope.net>2019-07-27 18:25:26 -0600
commit96ba6946096b2b22002a8c6416ef63d4d47cf90f (patch)
tree0562dc466d7c9d51e58ceb710ab83218d1d223ac /challenge-018
parentcf4e0810bafed04b13bb7872cb2bfcd1ae920864 (diff)
downloadperlweeklychallenge-club-96ba6946096b2b22002a8c6416ef63d4d47cf90f.tar.gz
perlweeklychallenge-club-96ba6946096b2b22002a8c6416ef63d4d47cf90f.tar.bz2
perlweeklychallenge-club-96ba6946096b2b22002a8c6416ef63d4d47cf90f.zip
Solutions for problem 17.1 in P6 and P5
Diffstat (limited to 'challenge-018')
-rwxr-xr-xchallenge-018/joelle-maslak/perl5/ch-1.pl56
-rwxr-xr-xchallenge-018/joelle-maslak/perl6/ch-1.p629
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-018/joelle-maslak/perl5/ch-1.pl b/challenge-018/joelle-maslak/perl5/ch-1.pl
new file mode 100755
index 0000000000..1d3e6e7421
--- /dev/null
+++ b/challenge-018/joelle-maslak/perl5/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+use v5.22;
+use strict;
+use warnings;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use autodie;
+use List::Util qw(max);
+
+sub main() {
+ die "Usage: $0 <m> .. <n>" if @ARGV < 2;
+
+ my @matchwords = intersection( map { substrings($_) } @ARGV );
+ if ( scalar(@matchwords) ) {
+ my $max = max map { length($_) } @matchwords;
+ my (@longwords) = grep { length($_) == $max } @matchwords;
+ say "Max length: $max; Substrings: " . join( " ", @longwords );
+ } else {
+ say "No substrings in common";
+ }
+
+ return;
+}
+
+sub substrings($word) {
+ my %ret;
+
+ for ( my $i = 0; $i < length($word); $i++ ) {
+ for ( my $j = 1; $j < ( length($word) + 1 - $i ); $j++ ) {
+ $ret{ substr( $word, $i, $j ) } = 1;
+ }
+ }
+
+ return [ keys %ret ];
+}
+
+sub intersection ( $first, $second, @sets ) {
+ my %hash1 = map { $_ => 1 } $first->@*;
+ my %hash2 = map { $_ => 1 } $second->@*;
+
+ my @common;
+ for my $k ( keys %hash1 ) {
+ push @common, $k if ( exists $hash2{$k} );
+ }
+
+ if ( scalar(@sets) ) {
+ return intersection( \@common, @sets );
+ } else {
+ return @common;
+ }
+}
+
+main();
diff --git a/challenge-018/joelle-maslak/perl6/ch-1.p6 b/challenge-018/joelle-maslak/perl6/ch-1.p6
new file mode 100755
index 0000000000..63f0a615d8
--- /dev/null
+++ b/challenge-018/joelle-maslak/perl6/ch-1.p6
@@ -0,0 +1,29 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN(+@words) {
+ my $matches = [∩] @words.map( { substrings($^a) } );
+ my @matchwords = $matches.keys.list;
+
+ if @matchwords.elems {
+ my $max = max @matchwords».chars;
+ my @longwords = @matchwords.grep: { $^a.chars == $max };
+ say "Max length: $max; Substrings: " ~ @longwords.join(" ");
+ } else {
+ say "No substrings in common";
+ }
+}
+
+sub substrings(Str() $word -->Set) {
+ my @chars = $word.comb;
+
+ return set gather {
+ for ^(@chars.elems) -> $i {
+ for $i .. (@chars.elems - 1) -> $j {
+ take @chars[$i .. $j].join;
+ }
+ }
+ }
+}
+
+