aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-16 06:51:11 +0100
committerGitHub <noreply@github.com>2019-06-16 06:51:11 +0100
commiteba59578a22155786df76ff4a69514f52adce6bb (patch)
tree5da3d20a612ca79068e2ba08c641a4322d4537e0
parent7a9a5d591c2bc3d66f16f14b055ea8ea922b8930 (diff)
parent90b5095355264da889396a4794d44cf48839c604 (diff)
downloadperlweeklychallenge-club-eba59578a22155786df76ff4a69514f52adce6bb.tar.gz
perlweeklychallenge-club-eba59578a22155786df76ff4a69514f52adce6bb.tar.bz2
perlweeklychallenge-club-eba59578a22155786df76ff4a69514f52adce6bb.zip
Merge pull request #255 from jmaslak/joelle-12-2-1
Solutions for Week 12 problem 2 in Perl 5 & Perl 6
-rwxr-xr-xchallenge-012/joelle-maslak/perl5/ch-2.pl33
-rwxr-xr-xchallenge-012/joelle-maslak/perl6/ch-2.p653
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-012/joelle-maslak/perl5/ch-2.pl b/challenge-012/joelle-maslak/perl5/ch-2.pl
new file mode 100755
index 0000000000..ee12c477dc
--- /dev/null
+++ b/challenge-012/joelle-maslak/perl5/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use v5.22;
+
+# Turn on method signatures
+use feature 'signatures';
+no warnings 'experimental::signatures';
+
+use List::Util qw(all);
+
+if ( @ARGV < 2 ) {
+ die "Must provide seperator and paths to find longest match";
+}
+
+my $seperator = shift @ARGV;
+my (@paths) = @ARGV;
+
+my (@path_parts) = sort { @$a <=> @$b }
+ map { [ split $seperator ] } @paths;
+
+my @common;
+my $first_dirs = shift @path_parts;
+
+for ( my $i = 0; $i < @$first_dirs; $i++ ) {
+ last unless all { $first_dirs->[$i] eq $_->[$i] } @path_parts;
+ push @common, $first_dirs->[$i];
+}
+
+say join $seperator, @common;
+
diff --git a/challenge-012/joelle-maslak/perl6/ch-2.p6 b/challenge-012/joelle-maslak/perl6/ch-2.p6
new file mode 100755
index 0000000000..351a23bb21
--- /dev/null
+++ b/challenge-012/joelle-maslak/perl6/ch-2.p6
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl6
+use v6;
+
+sub MAIN($seperator, +@paths) {
+ die "Must provide at least one path" if @paths.elems == 0;
+
+ if IO::Path.new('a').add('b').Str eq "a{$seperator}b" {
+ say find-common-io-path(@paths);
+ } else {
+ say find-common-str(@paths, $seperator);
+ }
+}
+
+sub find-common-str(@paths, $seperator) {
+ my @path-parts = @paths».split($seperator).sort: { $^a.elems <=> $^b.elems };
+
+ my @common;
+ my $first-dirs = @path-parts.shift;
+ for ^($first-dirs.elems) -> $i {
+ last unless $first-dirs[$i] eq @path-parts.map(*[$i]).all;
+ @common.push: $first-dirs[$i];
+ }
+
+ return @common.join($seperator);
+}
+
+sub find-common-io-path(@paths) {
+ my @io-paths = @paths.map: { IO::Path.new($_).resolve };
+
+ my @path-parts = gather {
+ for @io-paths -> $path is copy {
+ my @path-list;
+ while $path.parent.Str ne $path.Str {
+ @path-list.unshift: $path.Str;
+ $path = $path.parent;
+ }
+ take @path-list.unshift: $path;
+ }
+ }
+
+ @path-parts = @path-parts.sort( { $^a.elems <=> $^b.elems } );
+ my $first-dirs = @path-parts.shift;
+
+ my $common = '';
+ for ^($first-dirs.elems) -> $i {
+ last unless $first-dirs[$i] eq @path-parts.map(*[$i]).all;
+ $common = $first-dirs[$i];
+ }
+
+ return $common;
+}
+
+