diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-10-27 14:19:15 +0000 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2025-10-27 14:19:15 +0000 |
| commit | 7c0bca87cde4d66f357f42a9388d82166d8f9568 (patch) | |
| tree | 5992fc6adc0e3fb87c236c59105fba253420ffca | |
| parent | 9b1eba54d5e59d05df44f336120a6a03be62a645 (diff) | |
| download | perlweeklychallenge-club-7c0bca87cde4d66f357f42a9388d82166d8f9568.tar.gz perlweeklychallenge-club-7c0bca87cde4d66f357f42a9388d82166d8f9568.tar.bz2 perlweeklychallenge-club-7c0bca87cde4d66f357f42a9388d82166d8f9568.zip | |
Week 345 - Peak at the answers
| -rw-r--r-- | challenge-345/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-345/peter-campbell-smith/perl/ch-1.pl | 36 | ||||
| -rwxr-xr-x | challenge-345/peter-campbell-smith/perl/ch-2.pl | 52 |
3 files changed, 89 insertions, 0 deletions
diff --git a/challenge-345/peter-campbell-smith/blog.txt b/challenge-345/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..1800695822 --- /dev/null +++ b/challenge-345/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/345 diff --git a/challenge-345/peter-campbell-smith/perl/ch-1.pl b/challenge-345/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..01f705f5cb --- /dev/null +++ b/challenge-345/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-10-27 +use utf8; # Week 345 - task 1 - Peak positions +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +peak_positions(1, 2, 3, 2, 1); +peak_positions(1, 2, 3, 4, 5); +peak_positions(3, 3, 3, 3, 3); +peak_positions(3, 2, 1, 2, 3); +peak_positions(1, 2, 1, 3, 1, 4, 1, 6, 1); +peak_positions(1, 2); +peak_positions(-3, -2, -4, 0, 9, -100); + +sub peak_positions { + + my (@ints, @peaks, $j); + + # initialise + @ints = @_; + + # do as instructed + if ($#ints > 2) { + for $j (1 .. $#ints - 1) { + push @peaks, $j if ($ints[$j] > $ints[$j - 1] + and $ints[$j] > $ints[$j + 1]); + } + } + + say qq[\nInput: (] . join(', ', @ints) . ')'; + say qq[Output: (] . join(', ', @peaks) . ')'; +} diff --git a/challenge-345/peter-campbell-smith/perl/ch-2.pl b/challenge-345/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..2f822e28b7 --- /dev/null +++ b/challenge-345/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,52 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-10-27 +use utf8; # Week 345 - task 2 - Last visitor +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +last_visitor(5, -1, -1); +last_visitor(3, 7, -1, -1, -1); +last_visitor(2, -1, 4, -1, -1); +last_visitor(10, 20, -1, 30, -1, -1); +last_visitor(-1, -1, 5, -1); + +sub last_visitor { + + my (@ints, @ans, @seen, $i, $x); + + # initialise + @ints = @_; + $x = 0; + + for $i (0 .. $#ints) { + + # if $ints[i] is +ve push it onto @seen + if ($ints[$i] > 0) { + push @seen, $ints[$i]; + $x = 0; + + # if $ints[$i] is -1 ... + } elsif ($ints[$i] == -1) { + + # ... and $x <= length of @seen + if ($x <= $#seen) { + + # push $x'th from last element of @seen onto @ans + push @ans, $seen[$#seen - $x]; + } else { + + # else push -1 onto @ans + push @ans, -1; + } + + # increment no of consecutive -1s + $x ++; + } + } + say qq[\nInput: (] . join(', ', @ints) . ')'; + say qq[Output: (] . join(', ', @ans) . ')'; +} |
