diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-06-11 11:21:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-11 11:21:52 +0100 |
| commit | 304aaab374e2c78b73e10947c60ffe9fcd2d029c (patch) | |
| tree | 0d534460239b189cbdc0ddbc47920aac4f7f5bfe | |
| parent | 77fe3066c3a92633c3065bf44cc94ae2ce40b7ab (diff) | |
| parent | 292683ecf5c6be30485ad17856c0847f2d1ff89a (diff) | |
| download | perlweeklychallenge-club-304aaab374e2c78b73e10947c60ffe9fcd2d029c.tar.gz perlweeklychallenge-club-304aaab374e2c78b73e10947c60ffe9fcd2d029c.tar.bz2 perlweeklychallenge-club-304aaab374e2c78b73e10947c60ffe9fcd2d029c.zip | |
Merge pull request #243 from ajs/ajs-challenge-012
Aaron's solutions for 012.1 and 012.2 in P6
| -rw-r--r-- | challenge-012/aaron-sherman/README | 15 | ||||
| -rwxr-xr-x | challenge-012/aaron-sherman/perl6/ch-1.p6 | 31 | ||||
| -rwxr-xr-x | challenge-012/aaron-sherman/perl6/ch-2.p6 | 37 |
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-012/aaron-sherman/README b/challenge-012/aaron-sherman/README index 14ec31f570..08d5ec39d2 100644 --- a/challenge-012/aaron-sherman/README +++ b/challenge-012/aaron-sherman/README @@ -1 +1,16 @@ Solutions by Aaron Sherman. + +Challenge #1: The first Euclid non-prime + +This puzzle afforded me the opportunity to advertize a bit for the +Math::Sequences module by showing how a new entry would be defined. + +It otherwise simply prints the solution number uninterestingly. + +Challenge #2: Common path prefixes + +I thought that this would be a good chance to show off Perl 6's +user-defined operator features including the use of a user-defined +operator in a reduction meta-operator. + +The use of the program is simple and can be shown using --help diff --git a/challenge-012/aaron-sherman/perl6/ch-1.p6 b/challenge-012/aaron-sherman/perl6/ch-1.p6 new file mode 100755 index 0000000000..5f30300c62 --- /dev/null +++ b/challenge-012/aaron-sherman/perl6/ch-1.p6 @@ -0,0 +1,31 @@ +#!/usr/bin/env perl6 + +use v6.c; + +#Perl weekly challenge 012.1: + +# 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. + +# This solution serves two purposes. It both addresses the problem asked +# and demonstrates two sequences appropriate for inclusion in the +# Perl 6 OEIS library Math::Sequences + +#= OEIS sequence A010051: the primes +sub primes() { (2,3,*+2...*).grep: *.is-prime } +# Math::Sequences entry for the primes: +our @A010051 = lazy primes; + +#= OEIS sequence A057588: the Euclid numbers +sub euclids() { + gather for primes() -> $p { + take ((state $t=1) *= $p) + 1; + } +} +# Math::Sequences entry for the Euclids: +our @A057588 = lazy euclids; + +# Show the first non-prime Euclid number. +say euclids.grep(not *.is-prime).first; diff --git a/challenge-012/aaron-sherman/perl6/ch-2.p6 b/challenge-012/aaron-sherman/perl6/ch-2.p6 new file mode 100755 index 0000000000..a3e7d1fbbf --- /dev/null +++ b/challenge-012/aaron-sherman/perl6/ch-2.p6 @@ -0,0 +1,37 @@ +#!/usr/bin/env perl6 + +use v6.c; + +#Perl weekly challenge 012.2: + +# 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. + +# This solution demonstrates the creation of an infix operator and +# its use as a reduction meta-operator. Use --help for details + +use Test; + +sub infix:<common-prefix>(@a, @b) { + gather for @a Z @b -> ($a, $b) { + ($a ~~ $b and take $a but True) or last; + } +} + +sub common-leading-paths(@paths, :$separator='/') { + return join $separator, [common-prefix] @paths.map: *.split($separator); +} + +proto MAIN(|) {*} +multi MAIN(Bool :$test!) { + ok (<a b c> common-prefix <a b q>) ~~ <a b>, "common-prefix infix op"; + ok ([common-prefix] [<a b c>, <a b q r>, <a b>]) ~~ <a b>, "common-prefix reduction"; + ok common-leading-paths(<a$b a$c>, :separator<$>) eq 'a', "common-leading-paths with sep"; + # Example from the puzzle + ok common-leading-paths(</a/b/c/d /a/b/cd /a/b/cc /a/b/c/d/e>) eq '/a/b', "puzzle sample"; +} + +multi MAIN(Str :$separator='/', *@paths) { + say common-leading-paths @paths, :$separator; +} |
