Created
June 10, 2019 19:29
-
-
Save kevincolyer/2d22a6f3efa2502a8b2d553c2365ca38 to your computer and use it in GitHub Desktop.
Perl Weekly puzzle week 12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl6 | |
use v6; | |
use Test; | |
# 12.1 | |
my @primes = lazy (2,3,*+2 ... ∞).grep: *.is-prime; | |
# from wikipedia: https://en.wikipedia.org/wiki/Euclid_number | |
# Write a script that finds the smallest Euclid Number that is not prime | |
for ^∞ -> $n { | |
my $i = ( [*] @primes[0..$n] ) + 1; | |
if ! $i.is-prime { | |
say "12.1) (first $n primes + 1) -> $i is not prime"; | |
last; | |
} | |
}; | |
# Write a script that finds the common directory path, given a collection of paths and directory separator. For example, if the following paths are supplied. | |
# | |
# /a/b/c/d | |
# /a/b/cd | |
# /a/b/cc | |
# /a/b/c/d/e | |
# and the path separator is /. Your script should return /a/b as common directory path. | |
# | |
my @input=("/a/b/c/d", "/a/b/cd", "/a/b/cc", "/a/b/c/d/e"); | |
my $output="/a/b"; | |
my $sep="/"; | |
is shortestCDP(@input,$sep),$output,"Test of shortestCDP"; | |
is shortestCDP(("/a/b/c/d", "/a/b/cd", "/a/cc", "/a/b/c/d/e"),$sep),"/a","Test of shortestCDP"; | |
is shortestCDP(("/a/b/c/d", "/a/b/c", "/a/b/c/d/e"),$sep),"/a/b/c","Test of shortestCDP"; | |
done-testing; | |
say "12.2) shortest path is " ~ shortestCDP(@input,$sep); | |
sub shortestCDP(@input, $sep) { | |
my $l=@input[0].chars; | |
my @dirs; | |
for @input -> $i { | |
my $d=[$i.split($sep, :skip-empty)]; | |
$l=min($l, $d.elems); | |
@dirs.push($d); | |
} | |
my @found; | |
my $i=0; | |
while $i < $l { | |
my $alleq=True; | |
for @dirs -> $d { | |
next if $d[$i] eq @dirs[0][$i]; | |
$alleq=False; | |
last;; | |
} | |
$i++; | |
next if $alleq==False; | |
@found.push(@dirs[0][$i-1]); | |
} | |
return $sep ~ @found.join($sep); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment