diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-07-26 13:30:34 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-07-26 16:37:17 +0200 |
| commit | 373313e5bd3f5563f991fbd0a290b02f25cc3e91 (patch) | |
| tree | 98fae661d5da603af6e7a8051e658d4f7bd9557f /challenge-279 | |
| parent | 9a4dd9000bb589778b7f7b0036e1876ad6090c06 (diff) | |
| download | perlweeklychallenge-club-373313e5bd3f5563f991fbd0a290b02f25cc3e91.tar.gz perlweeklychallenge-club-373313e5bd3f5563f991fbd0a290b02f25cc3e91.tar.bz2 perlweeklychallenge-club-373313e5bd3f5563f991fbd0a290b02f25cc3e91.zip | |
Solution to task 1
Diffstat (limited to 'challenge-279')
| -rwxr-xr-x | challenge-279/jo-37/perl/ch-1.pl | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/challenge-279/jo-37/perl/ch-1.pl b/challenge-279/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..dc492b5b5a --- /dev/null +++ b/challenge-279/jo-37/perl/ch-1.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental 'signatures'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [-tests] [LETTERS WEIGHTS] + +-examples + run the examples from the challenge + +-tests + run some tests + +LETTERS + list of letters, optionally separated by commas + +WEIGHTS + list of weights, optionally separated by commas + +EOS + + +### Input and Output + +say sort_letters(map [split /,*/, $_], @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/07/26/ch-279.html#task-1 + + +sub sort_letters ($letters, $weights) { + (\my @word)->@[map $_ - 1, @$weights] = @$letters; + join '', @word; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is sort_letters(['R', 'E', 'P', 'L'], [3, 2, 1, 4]), + 'PERL', 'example 1'; + + is sort_letters(['A', 'U', 'R', 'K'], [2, 4, 1, 3]), + 'RAKU', 'example 2'; + + is sort_letters(['O', 'H', 'Y', 'N', 'P', 'T'], [5, 4, 2, 6, 1, 3]), + 'PYTHON', 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
