diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-07-31 23:58:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-07-31 23:58:16 +0100 |
| commit | f2eb3c291a8a368a41b1afa007bb39f9e2e20ca1 (patch) | |
| tree | b9dc206963b9f7133719edf685ee29937db40da7 | |
| parent | 059d8def9e7d907983456027e4cf9eec146c23d4 (diff) | |
| parent | bd5074fb310d4f64f48dcd2d6cbe187897321f1f (diff) | |
| download | perlweeklychallenge-club-f2eb3c291a8a368a41b1afa007bb39f9e2e20ca1.tar.gz perlweeklychallenge-club-f2eb3c291a8a368a41b1afa007bb39f9e2e20ca1.tar.bz2 perlweeklychallenge-club-f2eb3c291a8a368a41b1afa007bb39f9e2e20ca1.zip | |
Merge pull request #8480 from pjcs00/wk228
Week 228 submission
| -rw-r--r-- | challenge-228/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-228/peter-campbell-smith/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-228/peter-campbell-smith/perl/ch-2.pl | 47 |
3 files changed, 76 insertions, 0 deletions
diff --git a/challenge-228/peter-campbell-smith/blog.txt b/challenge-228/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..2e884314fc --- /dev/null +++ b/challenge-228/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/228 diff --git a/challenge-228/peter-campbell-smith/perl/ch-1.pl b/challenge-228/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..4bd5617017 --- /dev/null +++ b/challenge-228/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-31 +use utf8; # Week 228 task 1 - Unique sum +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use List::Uniq 'uniq'; + +my ($j, @int); + +unique_sum(2, 1, 3, 2); +unique_sum(1, 1, 1, 1); + +# longer example +for $j (1 .. 12) { + push (@int, int(rand(9) + 1)); +} +unique_sum(@int); + +sub unique_sum { + + my $sum; + $sum += $_ for uniq(@_); + + say qq[\nInput: \@int = (] . join(', ', @_) . ')'; + say qq[Output: ] . (defined($sum) ? $sum : 0); +} diff --git a/challenge-228/peter-campbell-smith/perl/ch-2.pl b/challenge-228/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d46fea1613 --- /dev/null +++ b/challenge-228/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-07-31 +use utf8; # Week 228 task 2 - Empty array +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +my ($j, @int); + +empty_array(3, 4, 2); +empty_array(1, 2, 3); +empty_array(1); +empty_array(); +empty_array(8, 3, 4, 7, 6, 2, 9, 1); + +sub empty_array { + + my (@array, $smallest, $operations, $explain); + + @array = @_; + $operations = 0; + $explain = ''; + while (1) { + + # if it's empty we're done + unless (@array) { + say qq[\nInput: \@int = (] . join (', ', @_) . ')'; + say qq[Output: $operations$explain]; + return; + } + + # if the first element is the smallest then remove it + $operations ++; + $smallest = $array[0]; + $smallest = $_ < $smallest ? $_ : $smallest for @array; + + if ($array[0] == $smallest) { + shift @array; + $explain .= qq[\n Operation $operations: remove element $smallest: (] . join(', ', @array) . ')'; + + # otherwise move it to the end + } else { + push(@array, shift @array); + $explain .= qq[\n Operation $operations: move element $array[-1] to the end: (] . join(', ', @array) . ')'; + } + } +} |
