aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-07-08 17:00:58 +0100
committerGitHub <noreply@github.com>2025-07-08 17:00:58 +0100
commit1e272a7972b48785d00a851aad8a13b200b043f2 (patch)
tree663cb0e17ad43467e44b830b2f5338ea627f4712
parent5f2927bf3cac5ae2913b06e60eb1cab572803f35 (diff)
parente86d9dfbd235ae360e4524110576718e8953e327 (diff)
downloadperlweeklychallenge-club-1e272a7972b48785d00a851aad8a13b200b043f2.tar.gz
perlweeklychallenge-club-1e272a7972b48785d00a851aad8a13b200b043f2.tar.bz2
perlweeklychallenge-club-1e272a7972b48785d00a851aad8a13b200b043f2.zip
Merge pull request #12302 from pjcs00/wk329
Week 329 - Fun with strings
-rw-r--r--challenge-329/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-329/peter-campbell-smith/perl/ch-1.pl43
-rwxr-xr-xchallenge-329/peter-campbell-smith/perl/ch-2.pl66
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;
+}