aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-01-27 14:46:29 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-01-27 14:46:29 +0000
commit959891b3d7cb1e37f7d1b60d1d5ac33590392a3a (patch)
tree7e1cf1dadd18ab0e09a524731573be5c4ccbcd83
parent906bb690f8e1d091ac358c4ac77818bf879144c8 (diff)
downloadperlweeklychallenge-club-959891b3d7cb1e37f7d1b60d1d5ac33590392a3a.tar.gz
perlweeklychallenge-club-959891b3d7cb1e37f7d1b60d1d5ac33590392a3a.tar.bz2
perlweeklychallenge-club-959891b3d7cb1e37f7d1b60d1d5ac33590392a3a.zip
w306 - Task 1 & 2
-rwxr-xr-xchallenge-306/perlboy1967/perl/ch1.pl44
-rwxr-xr-xchallenge-306/perlboy1967/perl/ch2.pl53
2 files changed, 97 insertions, 0 deletions
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<https://theweeklychallenge.org/blog/perl-weekly-challenge-305#TASK1>
+
+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<https://theweeklychallenge.org/blog/perl-weekly-challenge-305#TASK1>
+
+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;