diff options
| -rw-r--r-- | challenge-248/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-248/peter-campbell-smith/perl/ch-1.pl | 44 | ||||
| -rwxr-xr-x | challenge-248/peter-campbell-smith/perl/ch-2.pl | 57 |
3 files changed, 102 insertions, 0 deletions
diff --git a/challenge-248/peter-campbell-smith/blog.txt b/challenge-248/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..a03a6fdaef --- /dev/null +++ b/challenge-248/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/248 diff --git a/challenge-248/peter-campbell-smith/perl/ch-1.pl b/challenge-248/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..da6e352ff5 --- /dev/null +++ b/challenge-248/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +use v5.26; # The Weekly Challenge - 2023-12-18 +use utf8; # Week 248 task 1 - Shortest distance +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +shortest_distance('loveleetcode', 'e'); +shortest_distance('We wish you a merry christmas and a happy new year!', 'a'); +shortest_distance('zoos contain animals', 'z'); +shortest_distance('tornadoes have a vortex', 'x'); +shortest_distance('xylophones and zithers', 'q'); + +sub shortest_distance { + + my ($string, $char, @letters, $last, @count, @dist, $j, $count, $i, $k); + + ($string, $char) = @_; + + # initialise + @letters = split('', $string); + $last = @letters - 1; + $dist[$_] = 999 for 0 .. $last; + $count = 999; + + # go along string forwards and backwards + for $k (0, 1) { + for $j (0 .. $last) { + $i = $k ? $j : $last - $j; + + # if we've found a $char reset count to 0 + if ($letters[$i] eq $char) { + $count = $dist[$i] = 0; + + # or save the distance to the nearest $char + } else { + $count ++; + $dist[$i] = $count if $count < $dist[$i]; + } + } + } + say qq[\nInput: \@string = '$string', \$char = '$char']; + say qq[Output: ] . ($dist[0] != 999 ? join(', ', @dist) : qq['$char' does not occur in '$string']); +} diff --git a/challenge-248/peter-campbell-smith/perl/ch-2.pl b/challenge-248/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..eecc457542 --- /dev/null +++ b/challenge-248/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-12-18 +use utf8; # Week 242 task 2 - Submatrix sum +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +submatrix_sum([1, 2, 3, 4], + [5, 6, 7, 8], + [9, 10, 11, 12]); +submatrix_sum([37, 99, 43, 87, 22, 77, 0, 51, 20], + [18, 5, 67, 39, 52, 40, 39, 77, 1], + [93, 45, 34, 87, 12, 34, 15, 90, 22], + [11, 84, 45, 36, 75, 83, 40, 58, 89]); + +sub submatrix_sum { + + my ($in, $cols, $rows, $c, $r, $out, $chars, $max); + + # initialise + $in = \@_; + $cols = @{$in->[0]} - 1; + $rows = @$in - 1; + $max = 0; + + # loop over rows and columns calculating sum + for $r (0 .. $rows - 1) { + for $c (0 .. $cols - 1) { + $out->[$r]->[$c] = $in->[$r]->[$c] + $in->[$r + 1]->[$c] + + $in->[$r]->[$c + 1] + $in->[$r + 1]->[$c + 1]; + $chars = length($out->[$r]->[$c]); + $max = $chars if $chars > $max; + } + } + + # output the required data + print_matrix('Input: [', $in, $max); + print_matrix('Output: [', $out, $max); +} + + +sub print_matrix { + + my ($legend, $matrix, $j, $out, $max); + + ($legend, $matrix, $max) = @_; + + # format rows of matrix with numbers of equal width + $out = ''; + for $j (0 .. @$matrix - 1) { + $out .= qq[$legend] . join(', ', @{$matrix->[$j]}) . qq(]\n); + $legend = ' ['; + } + $out =~ s|(\d+)|sprintf("%${max}d", $1)|ge; + say $out; +} +
\ No newline at end of file |
