diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-06 15:22:15 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-06 15:22:15 +0100 |
| commit | 62e46aadf561766bd7eef5658be1631069412da1 (patch) | |
| tree | 8326b43e8b943aac560436ed4d717d49449f55e4 | |
| parent | a57412194c3bced3309d6bf04ca3c75c97697361 (diff) | |
| parent | 0e03099b6c57fb5b39f5614876129942230b4136 (diff) | |
| download | perlweeklychallenge-club-62e46aadf561766bd7eef5658be1631069412da1.tar.gz perlweeklychallenge-club-62e46aadf561766bd7eef5658be1631069412da1.tar.bz2 perlweeklychallenge-club-62e46aadf561766bd7eef5658be1631069412da1.zip | |
Merge pull request #11974 from choroba/ech320
Add solutions to 320: Maximum Count & Sum Difference by E. Choroba
| -rwxr-xr-x | challenge-320/e-choroba/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-320/e-choroba/perl/ch-2.pl | 16 |
2 files changed, 36 insertions, 0 deletions
diff --git a/challenge-320/e-choroba/perl/ch-1.pl b/challenge-320/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..3b224db3c8 --- /dev/null +++ b/challenge-320/e-choroba/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub maximum_count(@ints) { + my %count = (1 => 0, -1 => 0); + ++$count{ $_ <=> 0 } for @ints; + return (($count{1}) x 2, $count{-1})[ $count{1} <=> $count{-1} ] +} + +use Test::More tests => 3 + 3; + +is maximum_count(-3, -2, -1, 1, 2, 3), 3, 'Example 1'; +is maximum_count(-2, -1, 0, 0, 1), 2, 'Example 2'; +is maximum_count(1, 2, 3, 4), 4, 'Example 3'; + +is maximum_count(0, 0, 0), 0, 'All zeros'; +is maximum_count(-1, -1), 2, 'All negative'; +is maximum_count(1, 2, 0, 0, -1), 2, 'More positive'; diff --git a/challenge-320/e-choroba/perl/ch-2.pl b/challenge-320/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..06f5e074e7 --- /dev/null +++ b/challenge-320/e-choroba/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub sum_difference(@ints) { + abs(sum(@ints) - sum(map { split // } @ints)) +} + +use Test::More tests => 3; + +is sum_difference(1, 23, 4, 5), 18, 'Example 1'; +is sum_difference(1, 2, 3, 4, 5), 0, 'Example 2'; +is sum_difference(1, 2, 34), 27, 'Example 3'; |
