diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-13 13:16:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-13 13:16:02 +0100 |
| commit | 4098cbb09c4661f3a0d35d6e1d483843b6c914a2 (patch) | |
| tree | 166980909528713f449c7303e98064c62fbbcea4 | |
| parent | 4ce7dd1af1d512e1aaa9f56ab670c2df336615f7 (diff) | |
| parent | ee69e1eac12ef2e1013923cd1fd9bf1384c98aba (diff) | |
| download | perlweeklychallenge-club-4098cbb09c4661f3a0d35d6e1d483843b6c914a2.tar.gz perlweeklychallenge-club-4098cbb09c4661f3a0d35d6e1d483843b6c914a2.tar.bz2 perlweeklychallenge-club-4098cbb09c4661f3a0d35d6e1d483843b6c914a2.zip | |
Merge pull request #12012 from PerlBoy1967/branch-for-challenge-320
w320 - Task 1 & 2
| -rwxr-xr-x | challenge-320/perlboy1967/perl/ch1.pl | 39 | ||||
| -rwxr-xr-x | challenge-320/perlboy1967/perl/ch2.pl | 39 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-320/perlboy1967/perl/ch1.pl b/challenge-320/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..54b17cf0db --- /dev/null +++ b/challenge-320/perlboy1967/perl/ch1.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 320 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-320#TASK1> + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Maximum Count +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers. + +Write a script to return the maximum between the number of positive and +negative integers. Zero is neither positive nor negative. + +=cut + +use v5.32; +use common::sense; +use feature qw(signatures); +use Test2::V0 qw(-no_srand); +no warnings qw(experimental::signatures); + +use List::AllUtils qw(max); + +sub maximumCount (@ints) { + my %c; + $c{$_ <=> 0}++ for (@ints); + delete $c{0}; + return max(values %c); +} + +is(3,maximumCount(-3,-2,-1,1,2,3),'Example 1'); +is(2,maximumCount(-2,-1,0,0,1),'Example 2'); +is(4,maximumCount(1,2,3,4),'Example 3'); + +done_testing; diff --git a/challenge-320/perlboy1967/perl/ch2.pl b/challenge-320/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..dcf0ece082 --- /dev/null +++ b/challenge-320/perlboy1967/perl/ch2.pl @@ -0,0 +1,39 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 320 +L<https://theweeklychallenge.org/blog/perl-weekly-challenge-320#TASK2> + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Sum Difference +Submitted by: Mohammad Sajid Anwar + +You are given an array of positive integers. + +Write a script to return the absolute difference between digit sum and +element sum of 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); + +sub sumDifference (@ints) { + my ($eSum,$dSum) = (0,0); + for (@ints) { + $eSum += $_; + $dSum += $_ for (split//,$_); + } + return abs($eSum-$dSum); +} + +is(18,sumDifference(1,23,4,5),'Example 1'); +is(0,sumDifference(1,2,3,4,5),'Example 2'); +is(27,sumDifference(1,2,34),'Example 3'); + +done_testing; |
