aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-197')
-rw-r--r--challenge-197/dave-jacoby/perl/ch-1.pl28
-rw-r--r--challenge-197/dave-jacoby/perl/ch-2.pl33
2 files changed, 61 insertions, 0 deletions
diff --git a/challenge-197/dave-jacoby/perl/ch-1.pl b/challenge-197/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..ce02067ea2
--- /dev/null
+++ b/challenge-197/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+my @examples = ( [ 1, 0, 3, 0, 0, 5 ], [ 1, 6, 4 ], [ 0, 1, 0, 2, 0 ], );
+
+for my $e (@examples) {
+ my @list = $e->@*;
+ my @out = move_zero(@list);
+ my $list = join ', ', @list;
+ my $out = join ', ', @out;
+ say <<"END";
+ Input: \@list = ($list)
+ Output: ($out)
+END
+}
+
+sub move_zero( @list ) {
+ my @digit;
+ my @zero;
+ for my $i (@list) {
+ if ( $i > 0 ) { push @digit, $i }
+ else { push @zero, 0 }
+ }
+ return( @digit, @zero);
+}
diff --git a/challenge-197/dave-jacoby/perl/ch-2.pl b/challenge-197/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..4ad6065a0b
--- /dev/null
+++ b/challenge-197/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ say postderef signatures state };
+use Algorithm::Permute;
+
+my @examples = ( [ 1, 5, 1, 1, 6, 4 ], [ 1, 3, 2, 2, 3, 1 ], );
+
+for my $e (@examples) {
+ my @list = $e->@*;
+ my @out = wigglesort(@list);
+ my $list = join ', ', @list;
+ my $out = join ', ', @out;
+ say <<"END";
+ Input: \@list = ($list)
+ Output: ($out)
+END
+}
+
+sub wigglesort ( @list ) {
+ my $limit = -1 + scalar @list;
+ my $p = Algorithm::Permute->new( \@list );
+OUTER: while ( my @arr = $p->next ) {
+ for my $i ( grep { $_ % 2 == 1 } 0 .. $limit ) {
+ next OUTER if $arr[$i] <= $arr[ $i - 1 ];
+ next OUTER if defined $arr[ $i + 1 ] && $arr[$i] <= $arr[ $i + 1 ];
+ }
+ # say join ', ', @arr;
+ return @arr;
+ }
+ return ();
+}