aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2023-01-01 22:48:49 -0500
committerDave Jacoby <jacoby.david@gmail.com>2023-01-01 22:48:49 -0500
commit9beea0ef10b33004387b06d1bcea7267ea4309db (patch)
treefdfdda077d3cd91b598e0d87d8bbbb13b2c572c1
parentc08869ec78f2026fd0b6bb353524aa38a718c71c (diff)
downloadperlweeklychallenge-club-9beea0ef10b33004387b06d1bcea7267ea4309db.tar.gz
perlweeklychallenge-club-9beea0ef10b33004387b06d1bcea7267ea4309db.tar.bz2
perlweeklychallenge-club-9beea0ef10b33004387b06d1bcea7267ea4309db.zip
DAJ 19
-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 ();
+}