diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-10 09:18:09 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-10 09:18:09 +0100 |
| commit | a3969127afb2acd2c6834aba0cacc71b88ee6e17 (patch) | |
| tree | d2d05bfc6859dc3bf675aca674dc408dd2eb945d | |
| parent | d283e707e930790e2606ded8d91162d341f4b165 (diff) | |
| parent | db1699261a0d01d1be3e186ab862dde66873a688 (diff) | |
| download | perlweeklychallenge-club-a3969127afb2acd2c6834aba0cacc71b88ee6e17.tar.gz perlweeklychallenge-club-a3969127afb2acd2c6834aba0cacc71b88ee6e17.tar.bz2 perlweeklychallenge-club-a3969127afb2acd2c6834aba0cacc71b88ee6e17.zip | |
Merge pull request #12152 from PerlBoy1967/branch-for-challenge-325
w325 - Task 1 & 2
| -rwxr-xr-x | challenge-325/perlboy1967/perl/ch1.pl | 35 | ||||
| -rwxr-xr-x | challenge-325/perlboy1967/perl/ch2.pl | 47 |
2 files changed, 82 insertions, 0 deletions
diff --git a/challenge-325/perlboy1967/perl/ch1.pl b/challenge-325/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..941e10119d --- /dev/null +++ b/challenge-325/perlboy1967/perl/ch1.pl @@ -0,0 +1,35 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 325 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-325#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Consecutive One +Submitted by: Mohammad Sajid Anwar + +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. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::Util qw(max); + +sub consecutiveOne (@binary) { + return max (0, map { length } grep '1', split '0', join '', @binary); +} + +is(consecutiveOne(0,1,1,0,1,1,1),3,'Example 1'); +is(consecutiveOne(0,0,0,0),0,'Example 2'); +is(consecutiveOne(1,0,1,0,1,1),2,'Example 3'); + +done_testing; diff --git a/challenge-325/perlboy1967/perl/ch2.pl b/challenge-325/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..9e2ccbaa41 --- /dev/null +++ b/challenge-325/perlboy1967/perl/ch2.pl @@ -0,0 +1,47 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 325 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-325#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Final Price +Submitted by: Mohammad Sajid Anwar + +You are given an array of item prices. + +Write a script to find out the final price of each items in the given array. + +There is a special discount scheme going on. If there’s an item with a lower +or equal price later in the list, you get a discount equal to that later price +(the first one you find in order). + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::MoreUtils qw(firstidx); + +sub finalPrice (@prices) { + my @n; + + while (@prices) { + push(@n,shift(@prices)); + my $m = firstidx { $_ <= $n[-1] } @prices; + $n[-1] -= $prices[$m] if $m >= 0; + } + + return @n; +} + +is([finalPrice(8,4,6,2,3)],[4,2,4,2,3],'Example 1'); +is([finalPrice(1,2,3,4,5)],[1,2,3,4,5],'Example 2'); +is([finalPrice(7,1,1,5)],[6,0,1,5],'Example 3'); + +done_testing; |
