From b096b095b22e3d96c362527926dfc3053404f224 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Fri, 30 Aug 2024 13:31:22 +0200 Subject: Solution to task 2 --- challenge-284/jo-37/perl/ch-2.pl | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 challenge-284/jo-37/perl/ch-2.pl diff --git a/challenge-284/jo-37/perl/ch-2.pl b/challenge-284/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..5536c97cbc --- /dev/null +++ b/challenge-284/jo-37/perl/ch-2.pl @@ -0,0 +1,89 @@ +#!/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 <@{@$list2} = (); + my @part2; + for my $n (@$list1) { + if (exists $list2{$n}) { + $list2{$n}++; + } else { + push @part2, $n; + } + } + + [ + (map +($_) x $list2{$_}, @$list2), + (sort {$a <=> $b} @part2), + ]; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is relative_sort( [2, 3, 9, 3, 1, 4, 6, 7, 2, 8, 5], + [2, 1, 4, 3, 5, 6]), + [2, 2, 1, 4, 3, 3, 5, 6, 7, 8, 9], + 'example 1'; + is relative_sort( [3, 3, 4, 6, 2, 4, 2, 1, 3], + [1, 3, 2]), + [1, 3, 3, 3, 2, 2, 4, 4, 6], + 'example 2'; + is relative_sort( [3, 0, 5, 0, 2, 1, 4, 1, 1], + [1, 0, 3, 2]), + [1, 1, 1, 0, 0, 3, 2, 4, 5], + 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is relative_sort( [-20, 0, 1, 2], + [2, 1]), + [2, 1, -20, 0], + 'negative'; + } + + + done_testing; + exit; +} -- cgit