aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-09-28 16:57:24 +0100
committerGitHub <noreply@github.com>2022-09-28 16:57:24 +0100
commit50a12d158dcdf5daec14c7e3ec98177778aef865 (patch)
tree3c25118bc397ddb91ee102bab16e46d2a16ee324
parentc1c9824eb54f643636f196c705c66fbd1c16ce68 (diff)
parentc06774ff6a0b172a454c4aeeef1128bb1d5cc938 (diff)
downloadperlweeklychallenge-club-50a12d158dcdf5daec14c7e3ec98177778aef865.tar.gz
perlweeklychallenge-club-50a12d158dcdf5daec14c7e3ec98177778aef865.tar.bz2
perlweeklychallenge-club-50a12d158dcdf5daec14c7e3ec98177778aef865.zip
Merge pull request #6810 from pjcs00/wk184
Week 184's submissions
-rw-r--r--challenge-184/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-184/peter-campbell-smith/perl/ch-1.pl35
-rwxr-xr-xchallenge-184/peter-campbell-smith/perl/ch-2.pl48
3 files changed, 84 insertions, 0 deletions
diff --git a/challenge-184/peter-campbell-smith/blog.txt b/challenge-184/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..1fe0aea406
--- /dev/null
+++ b/challenge-184/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+https://pjcs-pwc.blogspot.com/2022/09/sequence-numbers-and-split-arrays.html
diff --git a/challenge-184/peter-campbell-smith/perl/ch-1.pl b/challenge-184/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..e2ebf07a73
--- /dev/null
+++ b/challenge-184/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-09-28
+# PWC 184 task 1
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given list of strings in the format aa9999 i.e. first 2 characters can be anything 'a-z'
+# followed by 4 digits '0-9'. Write a script to replace the first two characters with sequence starting
+# with '00', '01', '02' etc.
+
+# Blog:
+
+my (@lists, $list_ref, @list, $old_show, $new_show, $j);
+
+# inputs
+@lists = (['ab1234', 'cd5678', 'ef1342'], ['pq1122', 'rs3334']);
+
+# loop over inputs
+while ($list_ref = shift @lists) {
+ @list = @$list_ref;
+ $old_show = $new_show = '';
+
+ # loop over list elements
+ for $j (0 .. scalar @list - 1) {
+ $old_show .= qq['$list[$j]', ];
+ $new_show .= qq['] . sprintf('%02d%s', $j, substr($list[$j], 2)) . qq[', ];
+ }
+
+ # show the answer
+ say qq[\nInput: (] . substr($old_show, 0, -2) . qq[)] .
+ qq[\nOutput: (] . substr($new_show, 0, -2) . qq[)];
+}
diff --git a/challenge-184/peter-campbell-smith/perl/ch-2.pl b/challenge-184/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..9f3eb06ffa
--- /dev/null
+++ b/challenge-184/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Peter Campbell Smith - 2022-09-28
+# PWC 184 task 2
+
+use v5.28;
+use utf8;
+use warnings;
+
+# You are given list of strings containing 0-9 and a-z separated by spaces only.
+# Write a script to split the data into two arrays, one for integers and one for letters only.
+
+# Blog:
+
+my (@lists, $list, $char, $first, $second, $output, $j);
+
+# inputs
+@lists = (qq['a 1 2 b 0', '3 c 4 d'], qq['1 2', 'p q r', 's 3', '4 5 t']);
+
+for $list (@lists) {
+ say qq[\nInput: $list];
+ $first = $second = '[';
+
+ # loop over characters in list
+ while ($list =~ m|(.)|g) {
+ $char = $1;
+
+ # a digit, append it and a comma to $first
+ if ($char =~ m|[0-9]|) {
+ $first .= qq[$char,];
+
+ # a letter, append it and a comma to $second
+ } elsif ($char =~ m|[a-z]|i) {
+ $second .= qq['$char',];
+
+ # a comma, append '], [' to both $first and $second
+ } elsif ($char eq ',') {
+ $first .= '], [';
+ $second .= '], [';
+ }
+ }
+
+ # tweaks to eliminate empty [] pairs and convert ,] to ]
+ $output = qq([$first]] and [$second]]);
+ $output =~ s|,]|]|g;
+ $output =~ s|\[],\s*||g;
+ say qq[Output: $output];
+}