diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-12 10:22:39 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-03-23 18:03:39 +0100 |
| commit | 7231f6bb9d00c61c2c12a47e50bcff9420971f7e (patch) | |
| tree | d8983455f0e64ab4f86e12e6a75ba1bc89634b11 | |
| parent | 73c2861e3562b8e493511793edf9c0fba8c5b7f2 (diff) | |
| download | perlweeklychallenge-club-7231f6bb9d00c61c2c12a47e50bcff9420971f7e.tar.gz perlweeklychallenge-club-7231f6bb9d00c61c2c12a47e50bcff9420971f7e.tar.bz2 perlweeklychallenge-club-7231f6bb9d00c61c2c12a47e50bcff9420971f7e.zip | |
Challenge 006 task 1
| -rwxr-xr-x | challenge-006/jo-37/perl/ch-1.pl | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-006/jo-37/perl/ch-1.pl b/challenge-006/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..a54df67b3c --- /dev/null +++ b/challenge-006/jo-37/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl -s + +use v5.16; +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... + sorted list of numbers + +EOS + + +### Input and Output + +say compact(@ARGV); + + +### Implementation + +# Join list elements with commas, replace commas between successive +# numbers with hyphens, remove elements enclosed with hyphens and +# finally replace hyphens between successive numbers with commas. +sub compact { + join(',', @_) =~ s/(\d+)\K,(?=(\d+))/$2 == $1 + 1 ? '-' : ','/egr + =~ s/-\d+(?=-)//gr + =~ s/(\d+)\K-(?=(\d+))/$2 == $1 + 1 ? ',' : '-'/egr; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is compact(1,2,3,4,9,10,14,15,16), '1-4,9,10,14-16', 'example'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
