diff options
| author | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-06-10 09:59:27 +0100 |
|---|---|---|
| committer | Simon Proctor <simon.proctor@zpg.co.uk> | 2019-06-10 09:59:27 +0100 |
| commit | beece1a136152e7819ab4a8ff7f962df078cbd14 (patch) | |
| tree | deb3d8e39f58a72ab005fb975060fc5ba977ad6f | |
| parent | 0278536bf027030095cca4fd7163a04443307260 (diff) | |
| download | perlweeklychallenge-club-beece1a136152e7819ab4a8ff7f962df078cbd14.tar.gz perlweeklychallenge-club-beece1a136152e7819ab4a8ff7f962df078cbd14.tar.bz2 perlweeklychallenge-club-beece1a136152e7819ab4a8ff7f962df078cbd14.zip | |
Longest shared path
| -rwxr-xr-x | challenge-012/simon-proctor/perl6/ch-2.p6 | 20 |
1 files changed, 20 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..cbfd7b1c73 --- /dev/null +++ b/challenge-012/simon-proctor/perl6/ch-2.p6 @@ -0,0 +1,20 @@ +#!/usr/bin/perl6 + +use v6; + +my %*SUB-MAIN-OPTS = :named-anywhere; + +#| 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; +} |
