aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-06-10 11:19:45 +0100
committerGitHub <noreply@github.com>2019-06-10 11:19:45 +0100
commiteb36d6c010bde5a9965e4b599575fcd7b5243db0 (patch)
treedecf954865b31466ddb0bfbbe3f0a300df4861ba
parent5a935e8c8194a3a87e3d6600dca78f733657b281 (diff)
parent295001f32ec6fd9ef204c62b86af10ae50945597 (diff)
downloadperlweeklychallenge-club-eb36d6c010bde5a9965e4b599575fcd7b5243db0.tar.gz
perlweeklychallenge-club-eb36d6c010bde5a9965e4b599575fcd7b5243db0.tar.bz2
perlweeklychallenge-club-eb36d6c010bde5a9965e4b599575fcd7b5243db0.zip
Merge pull request #238 from Scimon/master
Longest shared path solution
-rwxr-xr-xchallenge-012/simon-proctor/perl6/ch-2.p624
1 files changed, 24 insertions, 0 deletions
diff --git a/challenge-012/simon-proctor/perl6/ch-2.p6 b/challenge-012/simon-proctor/perl6/ch-2.p6
new file mode 100755
index 0000000000..22170940f4
--- /dev/null
+++ b/challenge-012/simon-proctor/perl6/ch-2.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/perl6
+
+use v6;
+
+my %*SUB-MAIN-OPTS = :named-anywhere;
+
+multi sub MAIN is hidden-from-USAGE {
+ say $*USAGE;
+}
+
+#| Given a list of paths returns the longest shared path between them
+multi sub MAIN(
+ *@paths, #= List of paths to check
+ :$seperator = $*SPEC.dir-sep #= Optional directory seperator. Defaults to the system standard.
+) {
+ my @checks = @paths.sort( { $^b.codes <=> $^a.codes } )[0].split($seperator)[1..*-2];
+ my $path = $seperator;
+ my $new-path = $path;
+ while ( all( @paths ) ~~ / ^ $new-path / ) {
+ $path = $new-path;
+ $new-path ~= ( @checks.shift ~ $seperator );
+ };
+ say $path;
+}