From ed3aa35ad5698326c35e000f64439143d5c0c301 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 23 Jun 2025 13:00:46 +0200 Subject: Add solution to 327: Missing Integers & MAD by E. Choroba --- challenge-327/e-choroba/perl/ch-1.pl | 22 ++++++++++++++++++++++ challenge-327/e-choroba/perl/ch-2.pl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100755 challenge-327/e-choroba/perl/ch-1.pl create mode 100755 challenge-327/e-choroba/perl/ch-2.pl 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'; -- cgit