From c06774ff6a0b172a454c4aeeef1128bb1d5cc938 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Wed, 28 Sep 2022 16:51:02 +0100 Subject: Week 184's submissions --- challenge-184/peter-campbell-smith/blog.txt | 1 + challenge-184/peter-campbell-smith/perl/ch-1.pl | 35 ++++++++++++++++++ challenge-184/peter-campbell-smith/perl/ch-2.pl | 48 +++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 challenge-184/peter-campbell-smith/blog.txt create mode 100755 challenge-184/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-184/peter-campbell-smith/perl/ch-2.pl 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]; +} -- cgit