aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-06-15 18:24:09 +0200
committerGitHub <noreply@github.com>2025-06-15 18:24:09 +0200
commitda1a2cc687e3f46b31e66935ad859d9a1fb6adb9 (patch)
tree2513aaad4b14ead53e36946640219dc82722efbd
parentf7e2953f19bf7cf1758cbffb345faf276a05a8d7 (diff)
downloadperlweeklychallenge-club-da1a2cc687e3f46b31e66935ad859d9a1fb6adb9.tar.gz
perlweeklychallenge-club-da1a2cc687e3f46b31e66935ad859d9a1fb6adb9.tar.bz2
perlweeklychallenge-club-da1a2cc687e3f46b31e66935ad859d9a1fb6adb9.zip
Create ch-1.pl
-rw-r--r--challenge-325/wanderdoc/perl/ch-1.pl53
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-325/wanderdoc/perl/ch-1.pl b/challenge-325/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..2e46e9d12c
--- /dev/null
+++ b/challenge-325/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,53 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are given a binary array containing only 0 or/and 1.
+Write a script to find out the maximum consecutive 1 in the given array.
+
+Example 1
+
+Input: @binary = (0, 1, 1, 0, 1, 1, 1)
+Output: 3
+
+
+Example 2
+
+Input: @binary = (0, 0, 0, 0)
+Output: 0
+
+
+Example 3
+
+Input: @binary = (1, 0, 1, 0, 1, 1)
+Output: 2
+
+=cut
+
+use Test2::V0 -no_srand => 1;
+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');
+done_testing();
+
+sub consecutive_one
+{
+ my @arr = @_;
+ my @output = (0);
+ my $counter = 0;
+ for my $elm ( @arr )
+ {
+ if ( 1 == $elm )
+ {
+ $counter++;
+ }
+ else
+ {
+ push @output, $counter;
+ $counter = 0;
+ }
+ }
+ push @output, $counter; # last element.
+ return (sort { $b <=> $a } @output)[0];
+}