diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-06-24 23:44:45 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-24 23:44:45 +0100 |
| commit | e363f25dc95d7ca638480ec22e092ebc0f100cdc (patch) | |
| tree | b7dfef68447498eaa7dce21355259d4187a0c99a | |
| parent | 5d3cbaedcd479f50731b4e3f30ba5e292a347776 (diff) | |
| parent | ed3aa35ad5698326c35e000f64439143d5c0c301 (diff) | |
| download | perlweeklychallenge-club-e363f25dc95d7ca638480ec22e092ebc0f100cdc.tar.gz perlweeklychallenge-club-e363f25dc95d7ca638480ec22e092ebc0f100cdc.tar.bz2 perlweeklychallenge-club-e363f25dc95d7ca638480ec22e092ebc0f100cdc.zip | |
Merge pull request #12229 from choroba/ech327
Add solution to 327: Missing Integers & MAD by E. Choroba
| -rwxr-xr-x | challenge-327/e-choroba/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-327/e-choroba/perl/ch-2.pl | 34 |
2 files changed, 56 insertions, 0 deletions
diff --git a/challenge-327/e-choroba/perl/ch-1.pl b/challenge-327/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..42a4cbb649 --- /dev/null +++ b/challenge-327/e-choroba/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub missing_integers(@ints) { + my %missing; + @missing{1 .. @ints} = (); + delete @missing{@ints}; + return keys %missing +} + +use Test2::V0; +plan(3 + 1); + +is [missing_integers(1, 2, 1, 3, 2, 5)], bag { item($_) for 4, 6; end() }, + 'Example 1'; +is [missing_integers(1, 1, 1)], bag { item($_) for 2, 3; end() }, 'Example 2'; +is [missing_integers(2, 2, 1)], [3], 'Example 3'; + +is [missing_integers(5, 9)], bag { item($_) for 1, 2; ;end() }, + 'greater than n'; diff --git a/challenge-327/e-choroba/perl/ch-2.pl b/challenge-327/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..fb1656835f --- /dev/null +++ b/challenge-327/e-choroba/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub mad(@ints) { + my @sorted = sort { $a <=> $b } @ints; + my @mad = ($sorted[1] - $sorted[0]); + for my $i (1 .. $#sorted) { + my $diff = abs($sorted[$i] - $sorted[ $i - 1 ]); + if ($diff < $mad[0]) { + @mad = ($diff); + } + if ($diff == $mad[0]) { + push @mad, [@sorted[$i - 1, $i]]; + } + } + return @mad[1 .. $#mad] +} + +use Test2::V0; +plan(3); + +use experimental qw( signatures ); # Needed again after Test2::V0; +sub bagobags(@elements) { + bag { + item(bag { item($_) for @$_; end() }) for @elements; + end(); + } +} + +is [mad(4, 1, 2, 3)], bagobags([1,2], [2,3], [3,4]), 'Example 1'; +is [mad(1, 3, 7, 11, 15)], bagobags([1,3]), 'Example 2'; +is [mad(1, 5, 3, 8)], bagobags([1,3], [3,5]), 'Example 3'; |
