diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-21 12:38:18 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-05-21 16:06:12 +0200 |
| commit | 4e968a0a6ea4135e3ade5dedb901f24aca3ed823 (patch) | |
| tree | 55032e976aa84b5d469fcd4f6a50295baa6cc98c | |
| parent | 5180e8021a174f065f31630be9160c6b6453b2dd (diff) | |
| download | perlweeklychallenge-club-4e968a0a6ea4135e3ade5dedb901f24aca3ed823.tar.gz perlweeklychallenge-club-4e968a0a6ea4135e3ade5dedb901f24aca3ed823.tar.bz2 perlweeklychallenge-club-4e968a0a6ea4135e3ade5dedb901f24aca3ed823.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-217/jo-37/perl/ch-2.pl | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-217/jo-37/perl/ch-2.pl b/challenge-217/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..f6145ffd7b --- /dev/null +++ b/challenge-217/jo-37/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +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 maximum_number(@ARGV); + + +### Implementation + +# At first glance it looks as the reverse lexicographic order would +# solve the task, but this is disproved by example 5. Thus creating a +# "reverse concatenation order". +sub maximum_number { + join '', sort {$b . $a <=> $a . $b} @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is maximum_number(1, 23), 231, 'example 1'; + is maximum_number(10, 3, 2), 3210, 'example 2'; + is maximum_number(31, 2, 4, 10), 431210, 'example 3'; + is maximum_number(5, 11, 4, 1, 2), 542111, 'example 4'; + is maximum_number(1, 10), 110, 'example 5'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
