diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-10 20:42:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-10 20:42:36 +0100 |
| commit | 2be21f753a6bb9f80ccf3964084604d5e946dd23 (patch) | |
| tree | 6e4d7e3385d597ba98d7bc31ee0ef4c5d3b572e4 | |
| parent | 16e00a3701bcfc45420dbe0b93ff8093e28ff7ac (diff) | |
| parent | 3bace0ac92dcc5b1874c7d5d77fa359d48b2cb64 (diff) | |
| download | perlweeklychallenge-club-2be21f753a6bb9f80ccf3964084604d5e946dd23.tar.gz perlweeklychallenge-club-2be21f753a6bb9f80ccf3964084604d5e946dd23.tar.bz2 perlweeklychallenge-club-2be21f753a6bb9f80ccf3964084604d5e946dd23.zip | |
Merge pull request #240 from yzhernand/ch-012-yozen
Added solutions by Yozen Hernandez for challenges 1 and 2 for week 12
| -rw-r--r-- | challenge-012/yozen-hernandez/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-012/yozen-hernandez/perl5/ch-1.pl | 42 | ||||
| -rwxr-xr-x | challenge-012/yozen-hernandez/perl5/ch-2.pl | 56 |
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 ), "'"; |
