aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-04-29 18:29:27 +0100
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2024-04-29 18:29:27 +0100
commit618519eee2f262d2c6aa01017e5aa8098b5b64d7 (patch)
tree0d31abaf16eb916a0955e5559252d6ff841eac20
parent8af4e17dc115ce2e7a19f8fa11e70c799d2f6fb9 (diff)
downloadperlweeklychallenge-club-618519eee2f262d2c6aa01017e5aa8098b5b64d7.tar.gz
perlweeklychallenge-club-618519eee2f262d2c6aa01017e5aa8098b5b64d7.tar.bz2
perlweeklychallenge-club-618519eee2f262d2c6aa01017e5aa8098b5b64d7.zip
Week 267 ...
-rw-r--r--challenge-267/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-267/peter-campbell-smith/perl/ch-1.pl37
-rwxr-xr-xchallenge-267/peter-campbell-smith/perl/ch-2.pl64
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-267/peter-campbell-smith/blog.txt b/challenge-267/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..dd56762766
--- /dev/null
+++ b/challenge-267/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/267
diff --git a/challenge-267/peter-campbell-smith/perl/ch-1.pl b/challenge-267/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..93ad00cf18
--- /dev/null
+++ b/challenge-267/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-292
+use utf8; # Week 267 - task 1 - Product sign
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+product_sign(-1, -2, -3, -4, 3, 2, 1);
+product_sign(-1, -2, -3, 4, 3, 2, 1);
+product_sign(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11);
+
+sub product_sign {
+
+ my (@ints, $product, $int, $result);
+
+ @ints = @_;
+ say qq[\nInput: \@ints = (] . join(', ', @ints) . ')';
+
+ # method 1 - simple
+ $product = 1;
+ $product *= $_ for @ints;
+ say qq[Output1: ] . ($product ? $product / abs($product) : 0);
+
+ # method 2 - more efficient
+ $result = 1;
+ for $int (@ints) {
+ next if ($int > 0);
+ if ($int == 0) {
+ $result = 0;
+ last;
+ }
+ $result = -$result;
+ }
+ say qq[Output2: $result];
+}
diff --git a/challenge-267/peter-campbell-smith/perl/ch-2.pl b/challenge-267/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..a23142cc6f
--- /dev/null
+++ b/challenge-267/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,64 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2024-04-292
+use utf8; # Week 267 - task 2 - Line counts
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+
+line_counts('abcdefghijklmnopqrstuvwxyz',
+ [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]);
+line_counts('bbbcccdddaaa',
+ [ 4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
+ 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]);
+line_counts('thequickbrownfoxjumpsoverthelazydog',
+ [15, 27, 12, 28, 16, 29, 33, 21, 14, 29, 36, 13, 14,
+ 9, 18, 23, 22, 35, 27, 15, 25, 21, 19, 35, 23, 15]);
+
+sub line_counts {
+
+ my ($str, @widths, $ch, $limit, %pixels, $lines,
+ $chars, $j, $line, @lines, @line_length);
+
+ $str = $_[0];
+ @widths = @{$_[1]};
+ $limit = 100;
+
+ # set $pixels{$char} to the width of $char
+ for $j (0 .. 25) {
+ $pixels{chr(ord('a') + $j)} = $widths[$j];
+ }
+
+ # loop over characters in $str
+ $line = 0;
+ $chars = $limit;
+ while ($str =~ m|([a-z])|g) {
+
+ # needs a new line
+ $ch = $1;
+ if ($chars + $pixels{$ch} > $limit) {
+ $line ++;
+ $chars = $pixels{$ch};
+
+ # fits on current line
+ } else {
+ $chars += $pixels{$ch};
+ }
+
+ # save for explanation
+ $lines[$line] .= $ch;
+ $line_length[$line] += $pixels{$ch};
+ }
+
+ # results
+ say qq[\nInput: \@str = '$str'];
+ say qq[ \$widths = (] . join(', ', @widths[0 .. 12]) . ',';
+ say qq[ ] . join(', ', @widths[13 .. 25]) . ')';
+ say qq[Output: ($line, $chars)];
+ for $j (1 .. @lines - 1) {
+ say qq[ Line $j: $lines[$j] ($line_length[$j] pixels)];
+ }
+
+}