aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-30 23:46:09 +0000
committerGitHub <noreply@github.com>2022-12-30 23:46:09 +0000
commit17d43662962e52410999dc138dc9ba0a83d73079 (patch)
tree77444b4fade914ef97bd24e8cb3b345c2449f301
parent7acc561c7413ec602a2987a281deda8c98cda6e3 (diff)
parentada27029285f21654783cd1760a19ba3b1d45a9f (diff)
downloadperlweeklychallenge-club-17d43662962e52410999dc138dc9ba0a83d73079.tar.gz
perlweeklychallenge-club-17d43662962e52410999dc138dc9ba0a83d73079.tar.bz2
perlweeklychallenge-club-17d43662962e52410999dc138dc9ba0a83d73079.zip
Merge pull request #7326 from jo-37/contrib
Solutions to challenge 197
-rwxr-xr-xchallenge-197/jo-37/perl/ch-1.pl59
-rwxr-xr-xchallenge-197/jo-37/perl/ch-2.pl68
2 files changed, 127 insertions, 0 deletions
diff --git a/challenge-197/jo-37/perl/ch-1.pl b/challenge-197/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..3a9bdb9c70
--- /dev/null
+++ b/challenge-197/jo-37/perl/ch-1.pl
@@ -0,0 +1,59 @@
+#!/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...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say "(@{[move_zero(@ARGV)]})";
+
+
+### Implementation
+
+sub move_zero {
+ my $cnt;
+ # Count and suppress zeroes, then append the suppressed zeroes.
+ ((grep {$cnt += !$_; $_} @_), (0) x $cnt)
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [move_zero(1, 0, 3, 0, 0, 5)], [1, 3, 5, 0, 0, 0], 'example 1';
+ is [move_zero(1, 6, 4)], [1, 6, 4], 'example 2';
+ is [move_zero(0, 1, 0, 2, 0)], [1, 2, 0, 0, 0], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [move_zero(0, 0, 0)], [0, 0, 0], 'zeroes only';
+ is [move_zero(1, 0, -1)], [1, -1, 0], 'negative';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-197/jo-37/perl/ch-2.pl b/challenge-197/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..413ed8d0b0
--- /dev/null
+++ b/challenge-197/jo-37/perl/ch-2.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::MoreUtils qw(zip part);
+
+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...
+ list of numbers
+
+EOS
+
+
+### Input and Output
+
+say "@{[wiggle_sort(@ARGV)]}";
+
+
+### Implementation
+
+# Sort the list in descending order, split it in two and zip both parts.
+# For an odd sized list, the first part gets one more element. "zip"
+# will then produce a trailing "undef" that will be omitted by the
+# slice.
+# If the list contains too many replicas of the same element then there
+# is no valid wiggle sorted list and the produced result will silently
+# violate the constraints.
+sub wiggle_sort {
+ my $i;
+ (&zip(
+ part {$i++ < $#_ / 2}
+ sort {$b <=> $a} @_
+ ))[0 .. $#_];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+ is [wiggle_sort(qw(1 5 1 1 6 4))], [qw(1 6 1 5 1 4)], 'example 1';
+ is [wiggle_sort(qw(1 3 2 2 3 1))], [qw(2 3 1 3 1 2)], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ is [wiggle_sort(qw(1 2 3 4 5))], [qw(3 5 2 4 1)], 'odd size';
+ is [(wiggle_sort(qw(1 1 1 1 1 2 3 4)))[6, 7]], [1, 1], '1 violates';
+ is [(wiggle_sort(qw(1 2 3 4 4 4 4)))[0, 1]], [4, 4], '4 violates';
+ is [wiggle_sort(-1, 0, 1)], [0, 1, -1], 'negative';
+ }
+
+ done_testing;
+ exit;
+}