From 4fa050a9ab0aba28986c44ea13c56db00b602b9a Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Sat, 13 Apr 2024 14:48:04 +0100 Subject: Week 264 - a bit late ... --- challenge-264/peter-campbell-smith/blog.txt | 1 + challenge-264/peter-campbell-smith/perl/ch-1.pl | 37 +++++++++++++++++++ challenge-264/peter-campbell-smith/perl/ch-2.pl | 48 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 challenge-264/peter-campbell-smith/blog.txt create mode 100755 challenge-264/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-264/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-264/peter-campbell-smith/blog.txt b/challenge-264/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..f2e72672d8 --- /dev/null +++ b/challenge-264/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/264 diff --git a/challenge-264/peter-campbell-smith/perl/ch-1.pl b/challenge-264/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..a1860f14b6 --- /dev/null +++ b/challenge-264/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-08 +use utf8; # Week 264 - task 1 - Greatest English letter +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +greatest_english_letter('PeRlwEeKLy'); +greatest_english_letter('zebras live in London Zoo'); +greatest_english_letter('Antelopes like chocolate'); +greatest_english_letter('all lower case gives a null result'); +greatest_english_letter('the lower case can come fiRst'); +greatest_english_letter('maybe harder - yAaBbcCdDY'); + +sub greatest_english_letter { + + my ($str, $C, $c, $m); + + $str = shift; + say qq[\nInput: \$str = '$str']; + + # reverse sort characters of $str + $str = join('', sort { $b cmp $a } split('', $str)); + + # loop over lc chars in $str + while ($str =~ m|([a-z])|g) { + $C = uc($1); + + # test for uc same char + next unless $str =~ m|$1.*$C|; + say qq[Output: '$C']; + return; + } + say qq[Output: '']; +} diff --git a/challenge-264/peter-campbell-smith/perl/ch-2.pl b/challenge-264/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..9503d8b471 --- /dev/null +++ b/challenge-264/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2024-04-08 +use utf8; # Week 264 - task 2 - Target array +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; + +target_array([0, 1, 2, 3, 4], [0, 1, 2, 2, 1]); +target_array([1, 2, 3, 4, 0], [0, 1, 2, 3, 0]); +target_array([1], [0]); +target_array([0, 1, 2, 3, 4], [3, 0, 4, 3, 2]); +target_array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [9, 4, 2, 7, 5, 8, 1, 0, 3, 6]); +target_array([11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); + +sub target_array { + + my (@source, @indices, @target, $i, $j, $k, $hold, $value); + + @source = @{$_[0]}; + @indices = @{$_[1]}; + + # loop over indices + for $i (0 .. @indices - 1) { + $j = $indices[$i]; + $value = $target[$j]; + + # if the place in @target is occupied, move it and subsequent ones right + $k = $j; + while (defined $value) { + $hold = $target[$k]; + $target[$k] = $value; + $value = $hold; + $k ++; + } + $target[$j] = $source[$i]; + } + + # show results (with '?' for undefined - see analysis) + for ($j = 0; $j < @target; $j ++) { + $target[$j] = '?' unless defined $target[$j]; + } + + say qq[\nInput: \@source = (] . join(', ', @source) . ')'; + say qq[ \@indices = (] . join(', ', @indices) . ')'; + say qq[Output: (] . join(', ', @target) . ')'; +} -- cgit