diff options
| author | Bob Lied <boblied+github@gmail.com> | 2025-01-20 17:24:07 -0600 |
|---|---|---|
| committer | Bob Lied <boblied+github@gmail.com> | 2025-01-20 17:24:07 -0600 |
| commit | 9bc546b7c0523a0976a2af4cbc0b5b65c1dd70ae (patch) | |
| tree | 72f4e80e4958c1c10cf40e617304a0e4e9d98670 | |
| parent | aab5052474cb534790d075a20d7c79521d5b68f1 (diff) | |
| download | perlweeklychallenge-club-9bc546b7c0523a0976a2af4cbc0b5b65c1dd70ae.tar.gz perlweeklychallenge-club-9bc546b7c0523a0976a2af4cbc0b5b65c1dd70ae.tar.bz2 perlweeklychallenge-club-9bc546b7c0523a0976a2af4cbc0b5b65c1dd70ae.zip | |
Week 305 initial solutions
| -rw-r--r-- | challenge-305/bob-lied/README | 6 | ||||
| -rw-r--r-- | challenge-305/bob-lied/perl/ch-1.pl | 98 | ||||
| -rw-r--r-- | challenge-305/bob-lied/perl/ch-2.pl | 89 |
3 files changed, 190 insertions, 3 deletions
diff --git a/challenge-305/bob-lied/README b/challenge-305/bob-lied/README index a7ca268f89..77992b92ee 100644 --- a/challenge-305/bob-lied/README +++ b/challenge-305/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 304 by Bob Lied +Solutions to weekly challenge 305 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-304/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-304/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-305/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-305/bob-lied diff --git a/challenge-305/bob-lied/perl/ch-1.pl b/challenge-305/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..51e8645ebb --- /dev/null +++ b/challenge-305/bob-lied/perl/ch-1.pl @@ -0,0 +1,98 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2025, Bob Lied +#============================================================================= +# ch-1.pl Perl Weekly Challenge 305 Task 1 Binary Prefix +#============================================================================= +# You are given a binary array. +# Write a script to return an array of booleans where the partial +# binary number up to that point is prime. +# Example 1 Input: @binary = (1, 0, 1) +# Output: (false, true, true) +# 1 = 1 => false, 10 = 2 => true, 101 = 5 => false +# +# Example 2 Input: @binary = (1, 1, 0) +# Output: (false, true, false) +# 1 = 1 => false, 11 => 3 => true, 110 => 6 => false +# +# Example 3 Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1) +# Output: (false, true, true, false, false, true, false, false, +# false, false, false, false, false, false, false, false, false, +# false, false, true) +#============================================================================= + +use v5.40; + +use Math::Prime::Util qw/is_prime/; +use List::Util qw/reductions/; + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +my $logger; +{ + use Log::Log4perl qw(:easy); + Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ), + layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" }); + $logger = Log::Log4perl->get_logger(); +} +#============================================================================= + +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +say '(', join(", ", map { $_ ? "true": "false" } binPrefix(@ARGV)->@*), ')'; + +#============================================================================= +sub binPrefix(@binary) +{ + my $n = shift @binary; + my @isPrime = ( is_prime($n) == 2 ); + while ( defined(my $b = shift @binary) ) + { + $n = $n * 2 + $b; + push @isPrime, is_prime($n) == 2; + } + return \@isPrime; +} + +sub binPrefix_reduce(@binary) +{ + return [ map { is_prime($_) == 2 } reductions { ($a<<1) + $b } @binary ] +} + +sub runTest +{ + use Test2::V0; + + is( binPrefix(1,0,1), [false,true,true], "Example 1"); + is( binPrefix(1,1,0), [false,true,false], "Example 2"); + is( binPrefix(1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1), + [ false, true, true, false, false, true, false, false, false, false, + false, false, false, false, false, false, false, false, false, true ], + "Example 3"); + + is( binPrefix_reduce(1,0,1), [false,true,true], "Example 1"); + is( binPrefix_reduce(1,1,0), [false,true,false], "Example 2"); + is( binPrefix_reduce(1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1), + [ false, true, true, false, false, true, false, false, false, false, + false, false, false, false, false, false, false, false, false, true ], + "Example 3"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + my @binary = (1) x 64; + + cmpthese($repeat, { + shift => sub { binPrefix(@binary) }, + reduce => sub { binPrefix_reduce(@binary) }, + }); +} diff --git a/challenge-305/bob-lied/perl/ch-2.pl b/challenge-305/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..8d13cb2a4a --- /dev/null +++ b/challenge-305/bob-lied/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# Copyright (c) 2025, Bob Lied +#============================================================================= +# ch-2.pl Perl Weekly Challenge 305 Task 2 Alien Dictionary +#============================================================================= +# You are given a list of words and alien dictionary character order. +# Write a script to sort lexicographically the given list of words based +# on the alien dictionary characters. +# Example 1 Input: @words = ("perl", "python", "raku") +# @alien = qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/ +# Output: ("raku", "python", "perl") +# +# Example 2 Input: @words = ("the", "weekly", "challenge") +# @alien = qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/ +# Output: ("challenge", "the", "weekly") +#============================================================================= + +use v5.40; + + +use Getopt::Long; +my $Verbose = false; +my $DoTest = false; +my $Benchmark = 0; + +my @Dictionary = ('a' .. 'z'); +my $D; + +GetOptions("dictionary:s" => \$D, "test" => \$DoTest, + "verbose" => \$Verbose, "benchmark:i" => \$Benchmark); +my $logger; +{ + use Log::Log4perl qw(:easy); + Log::Log4perl->easy_init({ level => ($Verbose ? $DEBUG : $INFO ), + layout => "%d{HH:mm:ss.SSS} %p{1} %m%n" }); + $logger = Log::Log4perl->get_logger(); +} +#============================================================================= + +exit(!runTest()) if $DoTest; +exit( runBenchmark($Benchmark) ) if $Benchmark; + +if ( $D ) +{ + @Dictionary = split(//, $D); +} + +say '("', join('", "', alien(\@Dictionary, @ARGV)->@*), '")'; + +#============================================================================= +sub alien($dictionary, @words) +{ + state $ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWZYZabcdefghijklmnopqrstuvwxyz'; + + my $d = join("", @$dictionary); + $d = uc($d) . lc($d); + + my %translated; + for my $w ( @words ) + { + eval "\$translated{$w} = ((fc \$w) =~ tr/$ALPHABET/$d/r)"; + die $@ if $@; + } + return [ sort { $translated{$a} cmp $translated{$b} } @words ]; +} + +sub runTest +{ + use Test2::V0; + + is( alien( [ qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/ ], + qw/perl python raku/), [ qw/raku python perl/ ], "Example 1"); + + is( alien( [ qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/ ], + qw/the weekly challenge/), [ qw/challenge the weekly/ ], "Example 2"); + + done_testing; +} + +sub runBenchmark($repeat) +{ + use Benchmark qw/cmpthese/; + + cmpthese($repeat, { + label => sub { }, + }); +} |
