diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-04-16 15:07:59 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-04-19 18:48:49 +0200 |
| commit | 56528b46b31288e3ae49291d9f75d83e46ef711e (patch) | |
| tree | c3f159d34effab9339bb1a72d2a431ab712cf138 /challenge-055 | |
| parent | 2ea35671eb4c5a9db8352d42632e4d48e9a17dba (diff) | |
| download | perlweeklychallenge-club-56528b46b31288e3ae49291d9f75d83e46ef711e.tar.gz perlweeklychallenge-club-56528b46b31288e3ae49291d9f75d83e46ef711e.tar.bz2 perlweeklychallenge-club-56528b46b31288e3ae49291d9f75d83e46ef711e.zip | |
Challenge 055 task 2
Diffstat (limited to 'challenge-055')
| -rwxr-xr-x | challenge-055/jo-37/perl/ch-2.pl | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/challenge-055/jo-37/perl/ch-2.pl b/challenge-055/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..1f9109d0e7 --- /dev/null +++ b/challenge-055/jo-37/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use experimental qw(signatures); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [--] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of numbers + +EOS + + +### Input and Output + +main: { + local $" = ','; + local $, = ','; + say map "(@$_)", wave_array([sort {$b <=> $a} @ARGV], [], 1, [])->@*; +} + + +### Implementation + +# Recursive implementation: +# - If there is only one element in the list, it is appended to the +# current wave if it agrees with the direction. +# - The first element of the wave array cannot be the smallest / largest +# from the list as there would not be a suitable second element +# otherwise. +# - Loop over unseen numbers only that agree with the direction. +# - On recursion to the next level, remove the currently selected +# element, reverse the remaining and flip the direction. +sub wave_array ($list, $prefix, $dir, $result) { + if (@$list == 1) { + push @$result, [@$prefix, @$list] + if @$prefix && ($list->[0] <=> $prefix->[-1]) * $dir >= 0; + return; + } + my %seen_p; + for my $p (0 .. $#$list - 1) { + my @l = @$list; + my $lp = splice @l, $p, 1; + next if $seen_p{$lp}; + $seen_p{$lp} = 1; + next if @$prefix && ($lp <=> $prefix->[-1]) * $dir < 0; + wave_array([reverse @l], [@$prefix, $lp], -$dir, $result); + } + + $result; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + is wave_array([4, 3, 2, 1], [], 1, []), + bag {item [2, 1, 4, 3]; item [4,1,3,2]; etc;}, 'example'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
