diff options
| -rw-r--r-- | challenge-251/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-251/peter-campbell-smith/perl/ch-1.pl | 38 | ||||
| -rwxr-xr-x | challenge-251/peter-campbell-smith/perl/ch-2.pl | 117 |
3 files changed, 156 insertions, 0 deletions
diff --git a/challenge-251/peter-campbell-smith/blog.txt b/challenge-251/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4dce7e403d --- /dev/null +++ b/challenge-251/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/251 diff --git a/challenge-251/peter-campbell-smith/perl/ch-1.pl b/challenge-251/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..478f3c5960 --- /dev/null +++ b/challenge-251/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-08 +use utf8; # Week 251 task 1 - Concatenation value +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode STDOUT, ':utf8'; +my ($j, @ints); + +concatenation_value(6, 12, 25, 1); +concatenation_value(10, 7, 31, 5, 2, 2); +concatenation_value(1, 2, 10); + +for $j (0 .. 99) { + push(@ints, int(rand(99))); +} +concatenation_value(@ints); + +sub concatenation_value { + + my(@ints, $sum, $j, $k); + + # initialise + @ints = @_; + $sum = 0; + + # iterate from both ends + for $j (0 .. @ints) { + $k = @ints - $j - 1; + last if $k < $j; + $sum += ($k == $j ? $ints[$j] : $ints[$j] . $ints[$k]); + } + + # show result + say qq[\nInput: (] . join(', ', @ints) . ')'; + say qq[Output: $sum]; +} diff --git a/challenge-251/peter-campbell-smith/perl/ch-2.pl b/challenge-251/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d8d37a3015 --- /dev/null +++ b/challenge-251/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2024-01-08 +use utf8; # Week 251 task 2 - Lucky numbers +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode STDOUT, ':utf8'; + +lucky_numbers([[ 3, 7, 8], + [ 9, 11, 13], + [15, 16, 17]]); + +lucky_numbers([[ 1, 10, 4, 2], + [ 9, 3, 8, 7], + [15, 16, 17, 12]]); + +lucky_numbers([[7, 8], + [1, 2]]); + +lucky_numbers([[1, 2, 3], + [2, 1, 4], + [3, 4, 1]]); + +lucky_numbers([[7, 4, 4, 7], + [6, 5, 5, 6], + [6, 5, 5, 6], + [7, 4, 4, 7]]); + + +sub lucky_numbers { + + my ($r, $c, @lucky); + our ($m, @mins, @maxs); + @mins = @maxs = (); + + $m = shift; + + # loop over elements of matrix + for $r (0 .. @$m - 1) { + for $c (0 .. @{$m->[0]} - 1) { + + # save value if element is lucky + push(@lucky, $m->[$r]->[$c]) if (min_in_row($r, $c) and max_in_col($r, $c)); + } + } + say format_matrix(qq{\nInput: \@matrix = }, $m); + say qq[Output: ] . (@lucky > 0 ? join(', ', @lucky) : -1); + + sub min_in_row { + + my ($r, $c, $cx, $min_value); + + # return true/false if element $m($r, $c) is the minimum in its row + ($r, $c) = @_; + + # return saved value if we've been here before + return ($mins[$r] == $m->[$r]->[$c] ? 1 : 0) if defined $mins[$r]; + + # or determine and save it if not + $min_value = 99999; + for $cx (0 .. @{$m->[0]} - 1) { + $min_value = $m->[$r]->[$cx] if $m->[$r]->[$cx] < $min_value; + } + $mins[$r] = $min_value; + return $m->[$r]->[$c] == $min_value; + } + + sub max_in_col { + + my ($r, $c, $rx, $max_value); + + # return true/false if element $m($r, $c) is the maximum in its column + ($r, $c) = @_; + + # return saved value if we've been here before + return ($maxs[$c] == $m->[$r]->[$c] ? 1 : 0) if defined $maxs[$c]; + + # or determine and save it if not + $max_value = -99999; + for $rx (0 .. @$m - 1) { + $max_value = $m->[$rx]->[$c] if $m->[$rx]->[$c] > $max_value; + } + $maxs[$c] = $max_value; + return $m->[$r]->[$c] == $max_value; + } +} + +sub format_matrix { + + # format the output + my ($w, $m, $r, $c, $prefix, $width, $rubric, $spaces, $line); + + ($rubric, $m) = @_; + $spaces = length($rubric); + + # find maximum width of element as printed by Perl + $w = 0; + for $r (0 .. @$m - 1) { + for $c (0. .. @{$m->[0]} - 1) { + $width = length($m->[$r]->[$c]); + $w = $width if $width > $w; + } + } + + # construct and output each row of matrix + for $r (0 .. @$m - 1) { + $line = $rubric . '['; + for $c (0 .. @{$m->[0]} - 1) { + $line .= sprintf("%${w}d", $m->[$r]->[$c]) . ', '; + } + $line =~ s|, $|]|; + print $line; + say '' unless $r == @$m - 1; + $rubric = (' ' x ($spaces - 1)); + } +} |
