diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-12-31 12:57:58 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-12-31 13:32:54 +0100 |
| commit | d1f3a76036058e00d0952b3b2ed8d839c59ae106 (patch) | |
| tree | 9f0222f92cb7af2ac94efa1ea9120edf4b30646e | |
| parent | b888d24f0fb055dd0e570d1fe1967f25dd28d2fe (diff) | |
| download | perlweeklychallenge-club-d1f3a76036058e00d0952b3b2ed8d839c59ae106.tar.gz perlweeklychallenge-club-d1f3a76036058e00d0952b3b2ed8d839c59ae106.tar.bz2 perlweeklychallenge-club-d1f3a76036058e00d0952b3b2ed8d839c59ae106.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-249/jo-37/perl/ch-2.pl | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/challenge-249/jo-37/perl/ch-2.pl b/challenge-249/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..4d440602ff --- /dev/null +++ b/challenge-249/jo-37/perl/ch-2.pl @@ -0,0 +1,83 @@ +#!/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] [DI] + +-examples + run the examples from the challenge + +-tests + run some tests + +DI + a string consisting of 'D's and 'I's + +EOS + + +### Input and Output + +say "(@{[arrange_di(shift)]})"; + + +### Implementation + +# First we transform the DI-string into an array of zeroes and ones +# where a zero represents a decrement and a one an increment. Lets +# assume the DI-string and the corresponding array have a length of L, +# there are ND 'D's / zeroes and NI 'I's / ones. +# Then we create two sequences: +# - SD as the numbers 0 .. ND - 1 in descending order +# - SI the numbers ND .. L - 1 in ascending order. +# It can be seen that: +# - Every element of SI is larger than any element of SD. +# - Every element of SD is smaller than any element of SI. +# - Every but the last element of SI is smaller than its successor. +# - Every but the last element of DI is larger than its successor. +# Now we can construct a permutation with the required incremental / +# decremental behaviour by successively picking the first element from +# the corresponding sequence - except for the very first. +# The first element of our permutation may be chosen as follows: +# - If the first step is a decrement, we just take L as the first +# element. +# - Otherwise, we pick the first element from the incremental sequence SI +# and append L to SI + +sub arrange_di { + my @di = map tr/DI/01/r, split //, shift; + my @count = (0, 0); + $count[$_]++ for @di; + my @pool = ([reverse 0 .. $count[0] - 1], [$count[0] .. $#di]); + + ($di[0] ? + do {push $pool[1]->@*, scalar @di; shift $pool[1]->@*} : + scalar @di, + map shift $pool[$_]->@*, @di); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is [arrange_di('IDID')], [2, 3, 1, 4, 0], 'example 1'; + is [arrange_di('III')], [0, 1, 2, 3], 'example 2'; + is [arrange_di('DDI')], [3, 1, 0, 2], 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
