diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-12 10:05:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-12 10:05:31 +0100 |
| commit | 9c9a21b09e905457b595efda305e66b2a518a8b0 (patch) | |
| tree | 58474eacabef5552c6a84def88dc1e66dd5cf93c | |
| parent | 23c5f023851196407db9f28ca4a6d266fd8e9aa6 (diff) | |
| parent | 97c3fb1ea0920916d07c0451b98da2d3f57ca8d9 (diff) | |
| download | perlweeklychallenge-club-9c9a21b09e905457b595efda305e66b2a518a8b0.tar.gz perlweeklychallenge-club-9c9a21b09e905457b595efda305e66b2a518a8b0.tar.bz2 perlweeklychallenge-club-9c9a21b09e905457b595efda305e66b2a518a8b0.zip | |
Merge pull request #8358 from pjcs00/wk225
Week 225 - no recursion this time!
| -rw-r--r-- | challenge-225/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-225/peter-campbell-smith/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-225/peter-campbell-smith/perl/ch-2.pl | 39 |
3 files changed, 87 insertions, 0 deletions
diff --git a/challenge-225/peter-campbell-smith/blog.txt b/challenge-225/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..4dbd0c426f --- /dev/null +++ b/challenge-225/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/225 diff --git a/challenge-225/peter-campbell-smith/perl/ch-1.pl b/challenge-225/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..d6e6d4466c --- /dev/null +++ b/challenge-225/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-10 +use utf8; # Week 225 task 1 - Max words +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +binmode(STDOUT, ':utf8'); + +max_words('Perl and Raku belong to the same family.', + 'I love Perl.', + 'The Perl and Raku Conference.'); + +max_words('The Weekly Challenge.', + 'Python is the most popular guest language.', + 'Team PWC has over 300 members.'); + +max_words('Our solo sponsor Pete Sergeant has been a great support to keep us motivated.', + 'We are lucky that he agreed to continue the journey with us in the year 2023.', + 'I would like to personally thank Pete and his entire team for their generosity.', + 'It would be great if we could add few more to sponsor the prize money so that we could go back and + declare weekly champions as we have done in the past.', + 'I hope and wish this will become possible in 2023.', + 'The amount doesn’t have to be huge.', + 'However, it would be nice to show off bunch of supporters.', + 'If an organisation comes forward and supports us then that would be the ultimate achievement.'); + +sub max_words { + + my ($max, $sentence, $words, $rubric, $j); + + # loop over sentences + $max = 0; + while ($sentence = $_[$j++]) { + $rubric .= qq['$sentence',\n ]; + + # in scalar context s||| returns the number of substitutions made + $words = ($sentence =~ s|[^\s]+||g); + $max = $words if $words > $max; + } + + # show the results + $rubric =~ s|,\n +$||; + say qq[\nInput: \@list = ($rubric)]; + say qq[Output: $max]; +} + diff --git a/challenge-225/peter-campbell-smith/perl/ch-2.pl b/challenge-225/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..6d5eaaa4ab --- /dev/null +++ b/challenge-225/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-10 +use utf8; # Week 225 task 2 - Left right sum diff +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +left_right_sum_diff(10, 4, 8, 3); +left_right_sum_diff(1); +left_right_sum_diff(1, 2, 3, 4, 5); +left_right_sum_diff(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5); + +sub left_right_sum_diff { + + my (@ints, @stni, @lrsd, $count, $x); + + # initialise + @ints = @_; + $count = @ints - 1; + + # do the sums on @ints and the reverse of @ints + @stni = reverse @ints; + for $x (\@ints, \@stni) { + unshift @$x, 0; + pop @$x; + $x->[$_] += $x->[$_ - 1] for 1 .. $count; + } + @stni = reverse @stni; + + # do the differences + $lrsd[$_] = abs($ints[$_] - $stni[$_]) for 0 .. $count; + + # and spout + say qq[\nInput: \@ints = (] . join(', ', @_) . q[)] . + qq[\nOutput: \@left = (] . join(', ', @ints) . q[)] . + qq[\n \@right = (] . join(', ', @stni) . q[)] . + qq[\n \@lrsd = (] . join(', ', @lrsd) . q[)]; + +} |
