From 3bace0ac92dcc5b1874c7d5d77fa359d48b2cb64 Mon Sep 17 00:00:00 2001 From: Yozen Hernandez Date: Mon, 10 Jun 2019 15:29:03 -0400 Subject: Added solutions by Yozen Hernandez for challenges 1 and 2 for week 12 --- challenge-012/yozen-hernandez/blog.txt | 1 + challenge-012/yozen-hernandez/perl5/ch-1.pl | 42 ++++++++++++++++++++++ challenge-012/yozen-hernandez/perl5/ch-2.pl | 56 +++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 challenge-012/yozen-hernandez/blog.txt create mode 100755 challenge-012/yozen-hernandez/perl5/ch-1.pl create mode 100755 challenge-012/yozen-hernandez/perl5/ch-2.pl 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 ), "'"; -- cgit