diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-02-07 13:14:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-07 13:14:00 +0000 |
| commit | dc4364c8fa91f8c88198d1b15c5d588b5e3ac29b (patch) | |
| tree | bf4d207d4876caab178edffc5ff8c17a1106c853 | |
| parent | d8d086f2f50569758b94f39a6053e637a04f9db4 (diff) | |
| parent | 311137e4bfd18c9bbd546301f26468da3158aec7 (diff) | |
| download | perlweeklychallenge-club-dc4364c8fa91f8c88198d1b15c5d588b5e3ac29b.tar.gz perlweeklychallenge-club-dc4364c8fa91f8c88198d1b15c5d588b5e3ac29b.tar.bz2 perlweeklychallenge-club-dc4364c8fa91f8c88198d1b15c5d588b5e3ac29b.zip | |
Merge pull request #11539 from jo-37/contrib
Solutions to challenge 307
| -rw-r--r-- | challenge-307/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-307/jo-37/perl/ch-1.pl | 93 | ||||
| -rwxr-xr-x | challenge-307/jo-37/perl/ch-2.pl | 95 |
3 files changed, 189 insertions, 0 deletions
diff --git a/challenge-307/jo-37/blog.txt b/challenge-307/jo-37/blog.txt new file mode 100644 index 0000000000..58ed3290f2 --- /dev/null +++ b/challenge-307/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/02/07/ch-307.html diff --git a/challenge-307/jo-37/perl/ch-1.pl b/challenge-307/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..744d9dced3 --- /dev/null +++ b/challenge-307/jo-37/perl/ch-1.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 '!float'; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use PDL; + +### 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 - Check order + + usage: $0 [-examples] [-tests] [N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + N... + list of numbers + + EOS +} + + +### Input and Output + +say "(@{check_order(@ARGV)->unpdl})"; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/02/07/ch-307.html#task-1 + + +sub check_order { + my $ints = long \@_; + which $ints != $ints->qsort; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = check_order(@$args)->unpdl; + is $result, $expected, + "$name: (@$args) -> (@$expected)"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[5, 2, 4, 3, 1], [0, 2, 3, 4], 'example 1'], + [[1, 2, 1, 1, 3], [1, 3], 'example 2'], + [[3, 1, 3, 2, 3], [0, 1, 3], 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 3; + is check_order(2, 3, 5, 7, 11)->unpdl, [], 'sorted'; + is check_order()->unpdl, [], 'empty'; + is check_order(42)->unpdl, [], 'singleton'; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-307/jo-37/perl/ch-2.pl b/challenge-307/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..8826338789 --- /dev/null +++ b/challenge-307/jo-37/perl/ch-2.pl @@ -0,0 +1,95 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 '!float'; +use Test2::Tools::Subtest 'subtest_streamed'; +use Test2::Tools::PDL; +use Getopt::Long; +use experimental 'signatures'; +use utf8; + +use PDL; +use PDL::Char; + + +### 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 - Find anagrams + + usage: $0 [-examples] [-tests] [W...] + + -examples + run the examples from the challenge + + -tests + run some tests + + W... + list of words + + EOS +} + + +### Input and Output + +say count_anagrams(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/02/07/ch-307.html#task-2 + +sub count_anagrams { + 1 + PDL::Char->new(map fc, @_)->qsort->enumvecg->at(-1); +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = count_anagrams(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["acca", "dog", "god", "perl", "repl"], 3, 'example 1'], + [["abba", "baba", "aabb", "ab", "ab"], 2, 'example 2'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 3; + is count_anagrams(qw(ab bc ba)), 3, 'non-adjacent'; + is count_anagrams(qw(Ruffle Flufre)), 1, + 'fold case, ligature ffl'; + is count_anagrams(qw(ab bc cd dc)), 3, 'last group'; + }) : pass 'skip tests'; + + exit; +} |
