aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2024-05-13 17:04:34 -0400
committerDave Jacoby <jacoby.david@gmail.com>2024-05-13 17:04:34 -0400
commitec5ca1a8d465ef00f2841ce254ad3e8c70bbbf3d (patch)
treefcab08f26a062879824a83e755c1f8d8045738b6
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-ec5ca1a8d465ef00f2841ce254ad3e8c70bbbf3d.tar.gz
perlweeklychallenge-club-ec5ca1a8d465ef00f2841ce254ad3e8c70bbbf3d.tar.bz2
perlweeklychallenge-club-ec5ca1a8d465ef00f2841ce254ad3e8c70bbbf3d.zip
DAJ 269
-rw-r--r--challenge-269/dave-jacoby/perl/ch-1.pl34
-rw-r--r--challenge-269/dave-jacoby/perl/ch-2.pl40
2 files changed, 74 insertions, 0 deletions
diff --git a/challenge-269/dave-jacoby/perl/ch-1.pl b/challenge-269/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..058e9fbe77
--- /dev/null
+++ b/challenge-269/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ bitwise fc postderef say signatures state };
+
+my @examples = (
+
+ [ 1, 2, 3, 4, 5, ],
+ [ 2, 3, 8, 16, ],
+ [ 1, 2, 5, 7, 9, ],
+);
+for my $example (@examples) {
+ my $output = bitwise_or_fun(@$example);
+ $output = $output ? 'true' : 'false';
+ my $array = join ', ', $example->@*;
+ say <<"END";
+ Input: \@array = ($array)
+ Output: $output
+END
+}
+
+sub bitwise_or_fun (@array) {
+ my @copy = @array;
+ for my $i ( 0 .. -1 + scalar @array ) {
+ for my $j ( $i + 1 .. -1 + scalar @array ) {
+ my $x = $array[$i];
+ my $y = $array[$j];
+ my $z = $x | $y;
+ return 1 if $z % 2 == 0;
+ }
+ }
+ return 0;
+}
diff --git a/challenge-269/dave-jacoby/perl/ch-2.pl b/challenge-269/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..53fdabc8a2
--- /dev/null
+++ b/challenge-269/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use experimental qw{ fc say postderef signatures state };
+
+my @examples = (
+
+ [ 2, 1, 3, 4, 5 ],
+ [ 3, 2, 4 ],
+ [ 5, 4, 3, 8 ],
+);
+
+for my $example (@examples) {
+ my @output = distribute_elements( $example->@* );
+ my $output = join ', ', @output;
+ my $ints = join ', ', $example->@*;
+
+ say <<"END";
+ Input: \@ints = ($ints)
+ Output: ($output)
+END
+}
+
+sub distribute_elements (@ints) {
+ my @arr1;
+ my @arr2;
+ push @arr1, shift @ints;
+ push @arr2, shift @ints;
+
+ while ( @ints ) {
+ my $i = shift @ints;
+ if ( $arr1[-1] > $arr2[-1]) {
+ push @arr1, $i;
+ } else {
+ push @arr2, $i;
+ }
+ }
+ return @arr1,@arr2;
+}