diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-11-21 11:55:22 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2025-11-21 11:55:22 +0100 |
| commit | 94348f38b2323448b2728ed829b46df7efff90e2 (patch) | |
| tree | 6685d7b1bb46bc478f88ee68992b255bd0d0a19c | |
| parent | 70a976d79c7bc42ba97ceff0d21f6b95e19d8941 (diff) | |
| parent | 28221dd4720dd0529e4084590769251d4bbc6019 (diff) | |
| download | perlweeklychallenge-club-94348f38b2323448b2728ed829b46df7efff90e2.tar.gz perlweeklychallenge-club-94348f38b2323448b2728ed829b46df7efff90e2.tar.bz2 perlweeklychallenge-club-94348f38b2323448b2728ed829b46df7efff90e2.zip | |
Solutions to challenge 348
| -rw-r--r-- | challenge-348/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-348/jo-37/j/ch-2.ijs | 30 | ||||
| -rwxr-xr-x | challenge-348/jo-37/perl/ch-1.pl | 96 | ||||
| -rwxr-xr-x | challenge-348/jo-37/perl/ch-2.pl | 103 |
4 files changed, 230 insertions, 0 deletions
diff --git a/challenge-348/jo-37/blog.txt b/challenge-348/jo-37/blog.txt new file mode 100644 index 0000000000..c42ce0496c --- /dev/null +++ b/challenge-348/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/11/21/ch-348.html diff --git a/challenge-348/jo-37/j/ch-2.ijs b/challenge-348/jo-37/j/ch-2.ijs new file mode 100755 index 0000000000..03748ad55d --- /dev/null +++ b/challenge-348/jo-37/j/ch-2.ijs @@ -0,0 +1,30 @@ +#!/usr/local/bin/jconsole + +ConvertTime =: {{+/ 24 4 3 5 #: (24 * 60) | -~/ 24 60 #. > y}} +NB. ConvertTime =: +/ 24 4 3 5 #: (24 * 60) | -~/ (24 60)&#. + +3 : 0 > 2}. ARGV +if. + 2 = # y +do. + TR =: '([0-9]+):([0-9]+)' + try. + echo ConvertTime ". > {{((TR; 1 2) rxmatch y) rxfrom y}} "1 y + catch. + echo 'invalid arguments', 13!:12'' + end. +elseif. + 0 = # y +do. + echo ConvertTime 2 30; 2 45 + echo ConvertTime 11 55; 12 15 + echo ConvertTime 9 0; 13 0 + echo ConvertTime 23 45; 0 30 + echo ConvertTime 14 20; 15 25 +else. + echo 'Call "./ch-2.ijs H1:M1 H2:M2" to process times' + echo 'or "./ch-2.ijs" to run the examples' +end. +) + +exit '' diff --git a/challenge-348/jo-37/perl/ch-1.pl b/challenge-348/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..67937c9770 --- /dev/null +++ b/challenge-348/jo-37/perl/ch-1.pl @@ -0,0 +1,96 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; +use integer; + + +### 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 == 1; + +sub usage { + die <<~EOS; + $0 - string alike + + usage: $0 [-examples] [-tests] [STR] + + -examples + run the examples from the challenge + + -tests + run some tests + + STR + a string + + EOS +} + + +### Input and Output + +say +(qw(true false))[!string_alike(shift)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/11/21/ch-348.html#task-1 + +sub string_alike ($str) { + my $l = () = $str =~ /../g; + my ($h, $t) = map tr/aeiouAEIOU//, $str =~ /^(.{$l})((?1))$/; + + $h && $h == $t; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name, $reason=undef) { + my $todo = $reason ? todo $reason : undef; + my $result = string_alike(@$args); + is $result, $expected, + "$name: (@$args) -> " . $expected->name; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["textbook"], F(), 'example 1'], + [["book"], T(), 'example 2'], + [["AbCdEfGh"], T(), 'example 3'], + [["rhythmmyth"], F(), 'example 4'], + [["UmpireeAudio"], F(), 'example 5'], + ); + plan scalar @examples; + run_example @$_ for @examples; + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [["abcab"], F(), 'odd'], + ); + plan scalar @tests; + run_example @$_ for @tests; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-348/jo-37/perl/ch-2.pl b/challenge-348/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..5832d35888 --- /dev/null +++ b/challenge-348/jo-37/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/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::Util 'sum'; +use DateTime::Format::Strptime; + + +### 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 == 2; + +sub usage { + die <<~EOS; + $0 - convert time + + usage: $0 [-examples] [-tests] ] [SOURCE TARGET] + + -examples + run the examples from the challenge + + -tests + run some tests + + SOURCE TARGET + two time strings in the format HH:MM + + EOS +} + + +### Input and Output + +say convert_time(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/11/21/ch-348.html#task-2 + +sub convert_time { + state $div = [5, 3, 4, 24]; + state $parser = DateTime::Format::Strptime->new(pattern => "%R", + strict => 1, on_error => 'croak'); + + my ($s, $t) = map $parser->parse_datetime($_), @_; + my $diff = ($t - $s)->in_units('minutes') % (24 * 60); + + use integer; + sum map +($diff % $_, $diff /= $_)[0], @$div; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name, $reason=undef) { + my $todo = $reason ? todo $reason : undef; + my $result = convert_time(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["2:30", "2:45"], 1, 'example 1'], + [["11:55", "12:15"], 2, 'example 2'], + [["9:00", "13:00"], 4, 'example 3'], + [["23:45", "0:30"], 3, 'example 4'], + [["14:20", "15:25"], 2, 'example 5'], + ); + plan scalar @examples; + run_example @$_ for @examples; + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + my @tests = ( + [["12:00", "11:59"], 32, 'max'], + ); + plan scalar @tests; + run_example @$_ for @tests; + }) : pass 'skip tests'; + + exit; +} |
