aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-012/yozen-hernandez/blog.txt1
-rwxr-xr-xchallenge-012/yozen-hernandez/perl5/ch-1.pl42
-rwxr-xr-xchallenge-012/yozen-hernandez/perl5/ch-2.pl56
3 files changed, 99 insertions, 0 deletions
diff --git a/challenge-012/yozen-hernandez/blog.txt b/challenge-012/yozen-hernandez/blog.txt
new file mode 100644
index 0000000000..f5bb09a7a7
--- /dev/null
+++ b/challenge-012/yozen-hernandez/blog.txt
@@ -0,0 +1 @@
+https://yzhernand.github.io/posts/perl-weekly-challenge-12/ \ No newline at end of file
diff --git a/challenge-012/yozen-hernandez/perl5/ch-1.pl b/challenge-012/yozen-hernandez/perl5/ch-1.pl
new file mode 100755
index 0000000000..49974c10e9
--- /dev/null
+++ b/challenge-012/yozen-hernandez/perl5/ch-1.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw(say state);
+use Carp;
+
+# The numbers formed by adding one to the products of the smallest primes are
+# called the Euclid Numbers (see wiki). Write a script that finds the smallest
+# Euclid Number that is not prime. This challenge was proposed by Laurent
+# Rosenfeld.
+
+sub is_prime {
+ my $n = shift;
+
+ return 0 if grep { $n % $_ == 0 } ( 2 .. sqrt($n) );
+
+ return 1;
+}
+
+sub prime_iterator {
+ my $n = 1;
+ return sub {
+ 1 until is_prime ++$n;
+ return $n;
+ }
+}
+
+sub euclid_prime_iterator {
+ my $prime_iter = prime_iterator();
+ my $prime_prod = 1;
+ return sub {
+ $prime_prod *= $prime_iter->();
+ return $prime_prod + 1;
+ }
+}
+
+# scen = Smallest Composite Euclid Number
+my $iter = euclid_prime_iterator();
+my $scen = 0;
+$scen = $iter->() until ( is_prime($scen) == 0 );
+say $scen;
diff --git a/challenge-012/yozen-hernandez/perl5/ch-2.pl b/challenge-012/yozen-hernandez/perl5/ch-2.pl
new file mode 100755
index 0000000000..c3f277675f
--- /dev/null
+++ b/challenge-012/yozen-hernandez/perl5/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use Carp;
+
+# 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.
+
+sub find_common_path {
+ my ( $path_sep, $paths_ref ) = @_;
+ my @common_path;
+
+ for my $p ( @{$paths_ref} ) {
+ my @split_p = split /$path_sep/, $p;
+ unless (@common_path) {
+ @common_path = @split_p;
+ next;
+ }
+
+ if ( @split_p < @common_path ) {
+ @common_path = @common_path[ 0 .. @split_p - 1 ];
+ }
+
+ for my $i ( 0 .. @common_path - 1 ) {
+
+ # Stop processing this path as soon as we encounter
+ # a directory not seen before
+ if ( $split_p[$i] ne $common_path[$i] ) {
+
+ # Save only the so-far shared path.
+ # The array will always have one element
+ # thanks to the fact that the first separator
+ # is also the first character.
+ @common_path = @common_path[ 0 .. $i - 1 ];
+ last;
+ }
+ }
+
+ return ('') if ( @common_path == 1 && $common_path[0] eq '' );
+ }
+
+ return @common_path;
+}
+
+my @path_list = qw(/a/b/c/d
+ /a/b/cd
+ /a/b/cc
+ /a/b/c/d/e);
+
+my $path_sep = "/";
+my @common_path = find_common_path( $path_sep, \@path_list );
+
+say "Common path is: '", join( $path_sep, @common_path ), "'";