aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE. Choroba <choroba@matfyz.cz>2025-06-09 10:35:26 +0200
committerE. Choroba <choroba@matfyz.cz>2025-06-09 10:45:21 +0200
commit4683c1d1d5321cc8a5c2d960bd42437530acaed2 (patch)
treeb36c7d645905c57b8f48b780ec6b1d76d2df80e0
parente28477d2418099cfbb1a227133c69ddef6eed741 (diff)
downloadperlweeklychallenge-club-4683c1d1d5321cc8a5c2d960bd42437530acaed2.tar.gz
perlweeklychallenge-club-4683c1d1d5321cc8a5c2d960bd42437530acaed2.tar.bz2
perlweeklychallenge-club-4683c1d1d5321cc8a5c2d960bd42437530acaed2.zip
Add solutions to 325: Consecutive One & Final Price by E. Choroba
-rwxr-xr-xchallenge-325/e-choroba/perl/ch-1.pl22
-rwxr-xr-xchallenge-325/e-choroba/perl/ch-2.pl23
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-325/e-choroba/perl/ch-1.pl b/challenge-325/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..3ab474c1cd
--- /dev/null
+++ b/challenge-325/e-choroba/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub consecutive_one(@binary) {
+ my $l = my $max_l = 0;
+ for my $i (0 .. $#binary) {
+ if ($binary[$i]) {
+ $max_l = $l if ++$l > $max_l;
+ } else {
+ $l = 0;
+ }
+ }
+ return $max_l
+}
+
+use Test::More tests => 3;
+
+is consecutive_one(0, 1, 1, 0, 1, 1, 1), 3, 'Example 1';
+is consecutive_one(0, 0, 0, 0), 0, 'Example 2';
+is consecutive_one(1, 0, 1, 0, 1, 1), 2, 'Example 3';
diff --git a/challenge-325/e-choroba/perl/ch-2.pl b/challenge-325/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..9c82025312
--- /dev/null
+++ b/challenge-325/e-choroba/perl/ch-2.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub final_price(@prices) {
+ for my $i (0 .. $#prices - 1) {
+ for my $j ($i + 1 .. $#prices) {
+ if ($prices[$j] <= $prices[$i]) {
+ $prices[$i] -= $prices[$j];
+ last
+ }
+ }
+ }
+ return \@prices
+}
+
+use Test2::V0;
+plan(3);
+
+is final_price(8, 4, 6, 2, 3), [4, 2, 4, 2, 3], 'Example 1';
+is final_price(1, 2, 3, 4, 5), [1, 2, 3, 4, 5], 'Example 2';
+is final_price(7, 1, 1, 5), [6, 0, 1, 5], 'Example 3';