From db1699261a0d01d1be3e186ab862dde66873a688 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 9 Jun 2025 08:42:31 +0000 Subject: w325 - Task 1 & 2 --- challenge-325/perlboy1967/perl/ch1.pl | 35 ++++++++++++++++++++++++++ challenge-325/perlboy1967/perl/ch2.pl | 47 +++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100755 challenge-325/perlboy1967/perl/ch1.pl create mode 100755 challenge-325/perlboy1967/perl/ch2.pl 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 + +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 + +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; -- cgit