aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2022-12-26 14:38:36 +0000
committerGitHub <noreply@github.com>2022-12-26 14:38:36 +0000
commit684f9e08b3b18f4763e3634f00333bf35f4aebfa (patch)
tree8e93ded31f36caa1014d9d2314ae7dad8f069502
parentef4abf511640753973a09531e3d6e4324d1b0d77 (diff)
downloadperlweeklychallenge-club-684f9e08b3b18f4763e3634f00333bf35f4aebfa.tar.gz
perlweeklychallenge-club-684f9e08b3b18f4763e3634f00333bf35f4aebfa.tar.bz2
perlweeklychallenge-club-684f9e08b3b18f4763e3634f00333bf35f4aebfa.zip
Create ch-2.pl
-rw-r--r--challenge-197/james-smith/perl/ch-2.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/challenge-197/james-smith/perl/ch-2.pl b/challenge-197/james-smith/perl/ch-2.pl
new file mode 100644
index 0000000000..b16ae1d52e
--- /dev/null
+++ b/challenge-197/james-smith/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/local/bin/perl
+
+use strict;
+use warnings;
+use feature qw(say);
+use Test::More;
+
+my @TESTS = (
+ [ [1,1,2], 1 ],
+ [ [2,1,2], 0 ],
+ [ [1,1,1,2], 0 ],
+ [ [1,2,1,3,1,5], 1 ],
+ [ [1,2,1,3,1], 1 ],
+ [ [1,2], 1 ],
+ [ [2,1], 1 ],
+ [ [1], 1 ],
+ [ [1,5,1,1,6,4], 1 ],
+ [ [1,3,2,2,3,1], 1 ],
+ [ [2,3,1,3,1,2,1], 1 ],
+);
+
+
+is( check( wiggle_sort(@{$_->[0]}) ), $_->[1] ) for @TESTS;
+done_testing();
+
+sub wiggle_sort {
+ return @_ if @_<2;
+ @_ = sort { $a <=> $b } @_;
+ my @q = splice @_,(@_+1)/2;
+ return if @q < @_ && $_[1]==$q[0];
+ return map { @q ? ( $_ == $q[0] ? (return) : $_,shift @q ) : $_ } @_;
+}
+
+sub check {
+ return 0 unless @_;
+ my($t,$d) = (shift,1);
+ ($_<=>$t)!=$d?return 0:($t=$_,$d*=-1) for @_;
+ return 1;
+}
+