diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2025-05-12 09:29:46 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2025-05-12 09:29:46 +0000 |
| commit | ee69e1eac12ef2e1013923cd1fd9bf1384c98aba (patch) | |
| tree | b2e05feca0648edc9e90d66c52b4fb84e56ecf70 | |
| parent | 62f1ccaddfc5a65501df9cfdf528d28927fef410 (diff) | |
| download | perlweeklychallenge-club-ee69e1eac12ef2e1013923cd1fd9bf1384c98aba.tar.gz perlweeklychallenge-club-ee69e1eac12ef2e1013923cd1fd9bf1384c98aba.tar.bz2 perlweeklychallenge-club-ee69e1eac12ef2e1013923cd1fd9bf1384c98aba.zip | |
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; |
