diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-06-05 19:09:08 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-06-07 22:48:42 +0200 |
| commit | b6ef5e5a7ac86715dd704365108da399dadb8345 (patch) | |
| tree | 24018b8a3fa28721c47d8edc2ae99f3b5519744d | |
| parent | 3e4dc5350b29cea68cb7f32562e97463722ba1c0 (diff) | |
| download | perlweeklychallenge-club-b6ef5e5a7ac86715dd704365108da399dadb8345.tar.gz perlweeklychallenge-club-b6ef5e5a7ac86715dd704365108da399dadb8345.tar.bz2 perlweeklychallenge-club-b6ef5e5a7ac86715dd704365108da399dadb8345.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-272/jo-37/perl/ch-2.pl | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/challenge-272/jo-37/perl/ch-2.pl b/challenge-272/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..7f1ee3903f --- /dev/null +++ b/challenge-272/jo-37/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use PDL::Char; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [STR] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + enable trace output + +STR + an ASCII string + +EOS + + +### Input and Output + +say score(shift); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html#task-2 + + +sub score { + my $s = PDL::Char->new(shift); + sum abs long($s(0:-2)) - long($s(1:-1)); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is score("hello"), 13, "example 1"; + is score("perl"), 30, "example 2"; + is score("raku"), 37, "example 3"; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
