diff options
| -rw-r--r-- | challenge-329/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-329/peter-campbell-smith/perl/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-329/peter-campbell-smith/perl/ch-2.pl | 66 |
3 files changed, 110 insertions, 0 deletions
diff --git a/challenge-329/peter-campbell-smith/blog.txt b/challenge-329/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..1903132273 --- /dev/null +++ b/challenge-329/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/329 diff --git a/challenge-329/peter-campbell-smith/perl/ch-1.pl b/challenge-329/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..348f100c6f --- /dev/null +++ b/challenge-329/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-07-07 +use utf8; # Week 329 - task 1 - Counter integers +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +counter_integers('the1weekly2challenge2'); +counter_integers('go21od1lu5c7k'); +counter_integers('4p3e2r1l'); + +# longer example +my ($string, $chars); +$chars .= $_ for ('a' .. 'z', '0' .. '9'); +$string .= substr($chars, int(rand(36)), 1) for 0 .. 499; +counter_integers($string); + +sub counter_integers { + + my ($string, $result, $found); + + # initialise + $string = shift; + say qq[\nInput: '$string']; + + # replace non-digit sequences with blanks + $string =~ s|[a-z]| |gi; + + # create result in original order + $result = ', '; + while ($string =~ m|(\d+)|g) { + $found = $1 + 0; + if ($result !~ m|, $found,|) { + $result .= qq[$found, ]; + } + } + + # list them + say qq[Output: ] . substr($result, 2, -2); +} diff --git a/challenge-329/peter-campbell-smith/perl/ch-2.pl b/challenge-329/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..7ef7eacd01 --- /dev/null +++ b/challenge-329/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-07-07 +use utf8; # Week 329 - task 2 - Nice string +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +nice_string('YaaAho'); +nice_string('cC'); +nice_string('A'); +nice_string('ABCDEedcbaZABCDEedcbaa'); + +# longer example +my (@chars, $string); +@chars = (('A' .. 'I'),('a' .. 'i')); +$string .= $chars[int(rand(18))] for 0 .. 50; +nice_string($string); + +sub nice_string { + + my ($string, $c, $best, $longest, $s, $t, $try); + + # initialise + $string = shift; + $c = length($string); + $best = ''; + $longest = 0; + + # loop over starting points + for $s (0 .. $c - 2) { + + # loop up to remaining length + for $t (0 .. $c - $s) { + next unless $t >= $longest; + $try = substr($string, $s, $t); + + # nicest so far? + if (is_nice($try)) { + $best = $try; + $longest = $t; + } + } + } + + say qq[\nInput: '$string']; + say qq[Output: '$best']; +} + +sub is_nice { + + my ($string, @chars, $c, $C); + + # initialise + $string = shift; + @chars = split('', $string); + + # check for nicety + for $c (@chars) { + $C = $c gt 'Z' ? uc($c) : lc($c); + return 0 unless $string =~ m|$C|; + } + return 1; +} |
