From 959891b3d7cb1e37f7d1b60d1d5ac33590392a3a Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 27 Jan 2025 14:46:29 +0000 Subject: w306 - Task 1 & 2 --- challenge-306/perlboy1967/perl/ch1.pl | 44 +++++++++++++++++++++++++++++ challenge-306/perlboy1967/perl/ch2.pl | 53 +++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100755 challenge-306/perlboy1967/perl/ch1.pl create mode 100755 challenge-306/perlboy1967/perl/ch2.pl diff --git a/challenge-306/perlboy1967/perl/ch1.pl b/challenge-306/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..1cfc5fb8a0 --- /dev/null +++ b/challenge-306/perlboy1967/perl/ch1.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 305 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Odd Sum +Submitted by: Mohammad Sajid Anwar + +You are given an array of positive integers, @ints. + +Write a script to return the sum of all possible odd-length subarrays of +the given array. A subarray is a contiguous subsequence of the 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(sum); + +sub oddSum (@ints) { + my ($sum,$i) = (0,1); + while ($i <= scalar @ints) { + for my $j (0 .. scalar(@ints)-$i) { + $sum += sum(@ints[$j..$j+$i-1]) + } + $i += 2; + } + return $sum; +} + +is(oddSum(2,5,3,6,4),77,'Example 1'); +is(oddSum(1,3),4,'Example 2'); + +done_testing; diff --git a/challenge-306/perlboy1967/perl/ch2.pl b/challenge-306/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..64110e40e1 --- /dev/null +++ b/challenge-306/perlboy1967/perl/ch2.pl @@ -0,0 +1,53 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 305 +L + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Last Element +Submitted by: Mohammad Sajid Anwar + +You are given a array of integers, @ints. + +Write a script to play a game where you pick two biggest integers in the given array, +say x and y. Then do the following: + +a) if x == y then remove both from the given array +b) if x != y then remove x and replace y with (y - x) + +At the end of the game, there is at most one element left. + +Return the last element if found otherwise return 0. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); + +use Data::Printer; + +use Test2::V0 qw(-no_srand); + +no warnings qw(experimental::signatures); + +sub lastElement (@ints) { + my @s = sort { $a <=> $b } @ints; + while (@s > 1) { + my $i = pop(@s); + if (@s) { + my $j = pop(@s); + @s = sort { $a <=> $b } @s,$i-$j if ($i != $j); + return 0 unless @s; + } + } + return $s[-1]; +} + +is(lastElement(3,8,5,2,9,2),1,'Example 1'); +is(lastElement(3,2,5),0,'Example 2'); + +done_testing; -- cgit From cafec312a4999f37633d571e63139ea2efc92c20 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 27 Jan 2025 18:48:48 +0000 Subject: Task 2: Remove unused DDP --- challenge-306/perlboy1967/perl/ch2.pl | 2 -- 1 file changed, 2 deletions(-) diff --git a/challenge-306/perlboy1967/perl/ch2.pl b/challenge-306/perlboy1967/perl/ch2.pl index 64110e40e1..8493b55504 100755 --- a/challenge-306/perlboy1967/perl/ch2.pl +++ b/challenge-306/perlboy1967/perl/ch2.pl @@ -28,8 +28,6 @@ use v5.32; use common::sense; use feature qw(signatures); -use Data::Printer; - use Test2::V0 qw(-no_srand); no warnings qw(experimental::signatures); -- cgit From 49d72d0d8ac3440edcff0714e9433213075a7256 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 27 Jan 2025 18:50:31 +0000 Subject: Update comments --- challenge-306/perlboy1967/perl/ch1.pl | 4 ++-- challenge-306/perlboy1967/perl/ch2.pl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge-306/perlboy1967/perl/ch1.pl b/challenge-306/perlboy1967/perl/ch1.pl index 1cfc5fb8a0..402d471548 100755 --- a/challenge-306/perlboy1967/perl/ch1.pl +++ b/challenge-306/perlboy1967/perl/ch1.pl @@ -2,8 +2,8 @@ =pod -The Weekly Challenge - 305 -L +The Weekly Challenge - 306 +L Author: Niels 'PerlBoy' van Dijke diff --git a/challenge-306/perlboy1967/perl/ch2.pl b/challenge-306/perlboy1967/perl/ch2.pl index 8493b55504..564254d8a2 100755 --- a/challenge-306/perlboy1967/perl/ch2.pl +++ b/challenge-306/perlboy1967/perl/ch2.pl @@ -2,8 +2,8 @@ =pod -The Weekly Challenge - 305 -L +The Weekly Challenge - 306 +L Author: Niels 'PerlBoy' van Dijke -- cgit