From da1a2cc687e3f46b31e66935ad859d9a1fb6adb9 Mon Sep 17 00:00:00 2001 From: wanderdoc Date: Sun, 15 Jun 2025 18:24:09 +0200 Subject: Create ch-1.pl --- challenge-325/wanderdoc/perl/ch-1.pl | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-325/wanderdoc/perl/ch-1.pl 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]; +} -- cgit