diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-10 09:19:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-10 09:19:46 +0100 |
| commit | e8dfeb8c385b91def4279b7927ac3543c77ecec7 (patch) | |
| tree | 9fd5a12971ee05d4643d54684521360525d13b63 | |
| parent | e4713f7b26c48af9496dcd0a817ea870f5cc49e6 (diff) | |
| parent | 89cddd107c6406133bc339c5a0aac1b5ed9c02db (diff) | |
| download | perlweeklychallenge-club-e8dfeb8c385b91def4279b7927ac3543c77ecec7.tar.gz perlweeklychallenge-club-e8dfeb8c385b91def4279b7927ac3543c77ecec7.tar.bz2 perlweeklychallenge-club-e8dfeb8c385b91def4279b7927ac3543c77ecec7.zip | |
Merge pull request #12154 from pjcs00/wk325
Week 325 - Counts and discounts
| -rw-r--r-- | challenge-325/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-325/peter-campbell-smith/perl/ch-1.pl | 45 | ||||
| -rwxr-xr-x | challenge-325/peter-campbell-smith/perl/ch-2.pl | 39 |
3 files changed, 85 insertions, 0 deletions
diff --git a/challenge-325/peter-campbell-smith/blog.txt b/challenge-325/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..a2c1106c6c --- /dev/null +++ b/challenge-325/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://joppa:8083/challenge/325 diff --git a/challenge-325/peter-campbell-smith/perl/ch-1.pl b/challenge-325/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..6566b9d0c9 --- /dev/null +++ b/challenge-325/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-06-09 +use utf8; # Week 325 - task 1 - Consecutive ones +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +consecutive_ones(0, 1, 1, 0, 1, 1, 1); +consecutive_ones(0, 0, 0, 0); +consecutive_ones(1, 0, 1, 0, 1, 1); + +# bigger example +my @array; +push @array, int(rand($_)) & 1 for 0 .. 100; +consecutive_ones(@array); + +sub consecutive_ones { + + my(@array, $this_run, $max_run, $j); + + # initialise + @array = @_; + push @array, 0; + $max_run = $this_run = 0; + + # explore array + for $j (@array) { + + # it's a 1 + if ($j) { + $this_run ++; + $max_run = $this_run if $this_run > $max_run; + + # it's a 0 + } else { + $this_run = 0; + } + } + + say qq[\nInput: (] . join(', ', @array[0 .. $#array - 1]) . q[)]; + say qq[Output: $max_run]; +} diff --git a/challenge-325/peter-campbell-smith/perl/ch-2.pl b/challenge-325/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..d036c85480 --- /dev/null +++ b/challenge-325/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-06-09 +use utf8; # Week 325 - task 2 - Final price +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +final_price(8, 4, 6, 2, 3); +final_price(1, 2, 3, 4, 5); +final_price(7, 1, 1, 5); +final_price(3); +final_price(7, 7, 7, 7, 7); + +sub final_price { + + my(@prices, $j, $k); + + # initialise + @prices = @_; + say qq[\nInput: (] . join(', ', @prices) . q[)]; + + # check each price (except the last) + J: for $j (0 .. $#prices - 1) { + + # look over subsequent prices + for $k ($j + 1 .. $#prices) { + + # found a discounting one + if ($prices[$k] <= $prices[$j]) { + $prices[$j] -= $prices[$k]; + next J; + } + } + } + say qq[Output: (] . join(', ', @prices) . q[)]; +} |
