aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-05-13 14:27:27 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-05-13 14:27:27 +0100
commite072e86ffed64587063874696204fc268e79a23e (patch)
treec66184384a23a52cd68a67e85433260079da171b
parent02e309b3d451fff60404eb5ab3e539056f99ce0d (diff)
downloadperlweeklychallenge-club-e072e86ffed64587063874696204fc268e79a23e.tar.gz
perlweeklychallenge-club-e072e86ffed64587063874696204fc268e79a23e.tar.bz2
perlweeklychallenge-club-e072e86ffed64587063874696204fc268e79a23e.zip
Week 269 - Bits distribution
-rw-r--r--challenge-269/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-269/peter-campbell-smith/perl/ch-1.pl24
-rwxr-xr-xchallenge-269/peter-campbell-smith/perl/ch-2.pl34
3 files changed, 59 insertions, 0 deletions
diff --git a/challenge-269/peter-campbell-smith/blog.txt b/challenge-269/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..b4dcf788e4
--- /dev/null
+++ b/challenge-269/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/269
diff --git a/challenge-269/peter-campbell-smith/perl/ch-1.pl b/challenge-269/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..2f6d5f117c
--- /dev/null
+++ b/challenge-269/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-05-13
+use utf8; # Week 269 - task 1 - Bitwise or
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+bitwise_or(1, 2, 3, 4, 5);
+bitwise_or(2, 3, 9, 16);
+bitwise_or(1, 2, 5, 7, 9);
+bitwise_or(1, 3, 3, 7, 5, 9, 19, 20, 99, 100);
+bitwise_or(1, 3, 3, 7, 5, 9, 19, 20, 99, 101);
+
+sub bitwise_or {
+
+ # filter out even numbers
+ my @result = grep { ! ($_ & 1) } @_;
+
+ # there are at least 2 of them if $result[1] is defined
+ say sprintf(qq[\nInput: \@ints = (%s)], join(', ', @_));
+ say qq[Output: ] . ($result[1] ? qq[true ($result[0], $result[1])] : 'false');
+}
diff --git a/challenge-269/peter-campbell-smith/perl/ch-2.pl b/challenge-269/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..ba85648152
--- /dev/null
+++ b/challenge-269/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-05-13
+use utf8; # Week 269 - task 2 - Distribute elements
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+distribute_elements(2, 1, 3, 4, 5);
+distribute_elements(3, 2, 4);
+distribute_elements(5, 4, 3, 8);
+distribute_elements(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+distribute_elements(10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
+
+sub distribute_elements {
+
+ my (@ints, @arr1, @arr2, $j, $arr);
+
+ @ints = @_;
+
+ # push the first two elements of @ints onto @arr1 and @arr2
+ ($arr1[0], $arr2[0]) = @ints[0 .. 1];
+ $j = 2;
+
+ # push subsequent elements of @ints onto @arr1 or @arr2
+ do {
+ $arr = $arr1[-1] > $arr2[-1] ? \@arr1 : \@arr2;
+ push @$arr, $ints[$j];
+ } until ++ $j == @ints;
+
+ printf(qq[\nInput: \@ints = (%s)\n], join(', ', @ints));
+ printf(qq[Output: (%s)\n], join(', ', (@arr1, @arr2)));
+}