diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-09-27 02:08:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-09-27 02:08:23 +0100 |
| commit | 223fd8f1e5fe774dcaada9218c6ad9df07ddfc05 (patch) | |
| tree | 7fc682b0b1aa6933942641ce4d90a2c21206c671 | |
| parent | 251118ed929b561a045ddff35baec8a707b22eac (diff) | |
| parent | 7d694cd586d3d88fe73dcf7bdf9339e0275070a2 (diff) | |
| download | perlweeklychallenge-club-223fd8f1e5fe774dcaada9218c6ad9df07ddfc05.tar.gz perlweeklychallenge-club-223fd8f1e5fe774dcaada9218c6ad9df07ddfc05.tar.bz2 perlweeklychallenge-club-223fd8f1e5fe774dcaada9218c6ad9df07ddfc05.zip | |
Merge pull request #4929 from jo-37/contrib
Solution to challenge 130
| -rwxr-xr-x | challenge-130/jo-37/perl/ch-1.pl | 57 | ||||
| -rwxr-xr-x | challenge-131/jo-37/perl/ch-1.pl | 72 |
2 files changed, 129 insertions, 0 deletions
diff --git a/challenge-130/jo-37/perl/ch-1.pl b/challenge-130/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..eea4ff7c27 --- /dev/null +++ b/challenge-130/jo-37/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use List::Util qw(pairfirst); +use List::MoreUtils qw(frequency); +use experimental qw(signatures); + +our ($tests, $examples, $verbose); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [num ...] + +-examples + run the examples from the challenge + +-tests + run some tests + +num ... + numbers + +EOS + + +### Input and Output + +say odd_number(@ARGV); + + +### Implementation + +sub odd_number (@n) { + (pairfirst {$b % 2} frequency @n)[0]; +} + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is odd_number(2, 5, 4, 4, 5, 5, 2), 5, 'example 1'; + is odd_number(1, 2, 3, 4, 3, 2, 1, 4, 4), 4, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is odd_number(1, 1, 2, 2), U(), 'no odd number'; + } + + done_testing; + exit; +} diff --git a/challenge-131/jo-37/perl/ch-1.pl b/challenge-131/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..fdad67ebfe --- /dev/null +++ b/challenge-131/jo-37/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use List::Util 'reduce'; +use Data::Dump; +use experimental qw(signatures postderef); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [num ...] + +-examples + run the examples from the challenge + +-tests + run some tests + +num ... + numbers + + +EOS + + +### Input and Output +dd @{consecutive_arrays(@ARGV)}; + + +### Implementation + +sub consecutive_arrays($f, @a) { + reduce { + if ($b == $a->[-1][-1] + 1) { + push $a->[-1]->@*, $b; + } else { + push @$a, [$b]; + } + $a; + } [[$f]], @a; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is consecutive_arrays(1, 2, 3, 6, 7, 8, 9), + [[1, 2, 3], [6, 7, 8, 9]], 'example 1'; + is consecutive_arrays(11, 12, 14, 17, 18, 19), + [[11, 12], [14], [17, 18, 19]], 'example 2'; + is consecutive_arrays(2, 4, 6, 8), + [[2], [4], [6], [8]], 'example 3'; + is consecutive_arrays(1, 2, 3, 4, 5), + [[1, 2, 3, 4, 5]], 'example 4'; + } + + SKIP: { + skip "tests" unless $tests; + + is consecutive_arrays(1), + [[1]], 'single value'; + } + + done_testing; + exit; +} |
