diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-29 22:39:42 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-29 22:39:42 +0100 |
| commit | b1688c08ffdcf95b5b0c2660cd97b254e461fdbd (patch) | |
| tree | 77deee130b5c6e43b4e81d70f35515311bcc733b | |
| parent | 5b329eab3b34e2132988f8797bee4e7106e96d33 (diff) | |
| parent | f87a50b664117c344e49da8a01dddab337b47554 (diff) | |
| download | perlweeklychallenge-club-b1688c08ffdcf95b5b0c2660cd97b254e461fdbd.tar.gz perlweeklychallenge-club-b1688c08ffdcf95b5b0c2660cd97b254e461fdbd.tar.bz2 perlweeklychallenge-club-b1688c08ffdcf95b5b0c2660cd97b254e461fdbd.zip | |
Merge pull request #12589 from jo-37/contrib
Solutions to challenge 336
| -rw-r--r-- | challenge-336/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-336/jo-37/perl/ch-1.pl | 94 | ||||
| -rwxr-xr-x | challenge-336/jo-37/perl/ch-2.pl | 130 |
3 files changed, 225 insertions, 0 deletions
diff --git a/challenge-336/jo-37/blog.txt b/challenge-336/jo-37/blog.txt new file mode 100644 index 0000000000..444c499d3d --- /dev/null +++ b/challenge-336/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/08/29/ch-336.html diff --git a/challenge-336/jo-37/perl/ch-1.pl b/challenge-336/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..120d5dcaee --- /dev/null +++ b/challenge-336/jo-37/perl/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use List::MoreUtils 'frequency'; +use List::Util 'pairmap'; +use Math::Prime::Util 'gcd'; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - equal group + + usage: $0 [-examples] [-tests] [I...] + + -examples + run the examples from the challenge + + -tests + run some tests + + I... + list of items + + EOS +} + + +### Input and Output + +say +(qw(TRUE FALSE))[!equal_group(@ARGV)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/08/29/ch-336.html#task-1 + +sub equal_group { + 1 < gcd pairmap {$b} frequency @_; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = equal_group(@$args); + is $result, $expected, + "$name: (@$args) -> " . $expected->name; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[1,1,2,2,2,2], T(), 'example 1'], + [[1,1,1,2,2,2,3,3], F(), 'example 2'], + [[5,5,5,5,5,5,7,7,7,7,7,7], T(), 'example 3'], + [[1,2,3,4], F(), 'example 4'], + [[8,8,9,9,10,10,11,11], T(), 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + pass 'no tests'; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-336/jo-37/perl/ch-2.pl b/challenge-336/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..136d2bad93 --- /dev/null +++ b/challenge-336/jo-37/perl/ch-2.pl @@ -0,0 +1,130 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - final score + + usage: $0 [-examples] [-tests] [-- S...] + + -examples + run the examples from the challenge + + -tests + run some tests + + S... + list of scores + + EOS +} + + +### Input and Output + +say final_score(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/08/29/ch-336.html#task-1 + +package Score; + +sub new ($class) { + bless [], $class; +} + +sub total ($self) { + List::Util::sum0 @$self; +} + +sub apply ($self, @scores) { + for (@scores) { + push(@$self, $_), next if /^[-+]?\d+$/; + pop(@$self), next if /^C$/; + push(@$self, 2 * ($self->[-1] // 0)), next if /^D$/; + push(@$self, ($self->[-2] // 0) + ($self->[-1] // 0)), next if /^\+$/; + die "score invalid: $_"; + } + $self; +} + + +package main; + +sub final_score { + Score->new->apply(@_)->total; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = final_score(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["5","2","C","D","+"], 30, 'example 1'], + [["5","-2","4","C","D","9","+","+"], 27, 'example 2'], + [["7","D","D","C","+","3"], 45, 'example 3'], + [["-5","-10","+","D","C","+"], -55, 'example 4'], + [["3","6","+","D","C","8","+","D","-2","C","+"], 128, 'example 5'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 9; + + like dies {Score->new->apply('X')}, qr/score invalid/, + 'score invalid'; + is(Score->new->total, 0, 'empty stack'); + is(Score->new->apply('+')->total, 0, 'zero summands'); + is(Score->new->apply('1')->apply('+')->total, 2, 'one summand'); + is(Score->new->apply('D')->total, 0, 'double nothing'); + is(Score->new->apply('C')->total, 0, 'cancel on empty stack'); + { + my $x = Score->new; + $x->apply(3); + $x->apply(2); + $x->apply('+'); + is $x->total, 10, 'single scores'; + } + is(Score->new->apply(3)->apply(2)->apply('+')->total, 10, 'chained'); + is(Score->new->apply(qw(3 2 +))->total, 10, 'multiple scores'); + }) : pass 'skip tests'; + + exit; +} |
