From 45f5fda1003daa2327d53fe9e6ab4394651c00d8 Mon Sep 17 00:00:00 2001 From: Matt Martini Date: Thu, 11 Dec 2025 15:16:13 -0500 Subject: [PATCH] solutions for challenge 351 --- challenge-351/matt-martini/README | 1 + challenge-351/matt-martini/perl/ch-1.pl | 45 +++++++++++++++++++++++++ challenge-351/matt-martini/perl/ch-2.pl | 41 ++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 challenge-351/matt-martini/README create mode 100644 challenge-351/matt-martini/perl/ch-1.pl create mode 100644 challenge-351/matt-martini/perl/ch-2.pl diff --git a/challenge-351/matt-martini/README b/challenge-351/matt-martini/README new file mode 100644 index 0000000000..4407e4c65b --- /dev/null +++ b/challenge-351/matt-martini/README @@ -0,0 +1 @@ +Solution by Matt Martini diff --git a/challenge-351/matt-martini/perl/ch-1.pl b/challenge-351/matt-martini/perl/ch-1.pl new file mode 100644 index 0000000000..d15fbcbb1c --- /dev/null +++ b/challenge-351/matt-martini/perl/ch-1.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# You are given an array of integers. +# Write a script to return the average excluding the minimum and maximum +# of the given array. + +use 5.018; +use strict; +use warnings; +use Test2::V0; +use List::Util qw(min max sum0); + +plan tests => 5; + +sub special_average { + my (@ints) = @_; + + print 'Input: @ints = (' . join( ', ', @ints ) . ")\n"; + + my $min = min @ints; + my $max = max @ints; + + @ints = grep { !/$min|$max/ } @ints; + + my $sum = sum0 @ints; + my $count = scalar @ints || 1; + my $average = $sum / $count; + + say 'Output: ' . $average; + + # say 'Min ' . $min; + # say 'Max ' . $max; + # say 'Avg ' . $average; + + return $average; +} + +is( special_average( ( 8000, 5000, 6000, 2000, 3000, 7000 ) ), + 5250, 'example 1' ); +is( special_average( ( 100_000, 80_000, 110_000, 90_000 ) ), + 95_000, 'example 2' ); +is( special_average( ( 2500, 2500, 2500, 2500 ) ), 0, 'example 3' ); +is( special_average( (2500) ), 0, 'example 4' ); +is( special_average( ( 1000, 2000, 3000, 4000, 5000, 6000 ) ), + 3500, 'example 5' ); diff --git a/challenge-351/matt-martini/perl/ch-2.pl b/challenge-351/matt-martini/perl/ch-2.pl new file mode 100644 index 0000000000..3c45a10e76 --- /dev/null +++ b/challenge-351/matt-martini/perl/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl + +# You are given an array of numbers. +# Write a script to return true if the given array can be re-arranged +# to form an arithmetic progression, otherwise return false. + +# A sequence of numbers is called an arithmetic progression if the +# difference between any two consecutive elements is the same. + +use 5.018; +use strict; +use warnings; +use Test2::V0; + +plan tests => 5; + +sub arithmetic_progression { + my (@num) = @_; + print 'Input: @num = (' . join( ', ', @num ) . ")\n"; + + @num = sort { $a <=> $b } @num; + my $diff = $num[1] - $num[0]; + + my $result = 'true'; + COMPARE: + foreach ( my $i = 1; $i <= $#num; $i++ ) { + if ( $num[ $i - 1 ] + $diff != $num[$i] ) { + $result = 'false'; + last COMPARE; + } + } + + say 'Output: ' . $result; + return $result; +} + +is( arithmetic_progression( ( 1, 3, 5, 7, 9 ) ), 'true', 'example 1' ); +is( arithmetic_progression( ( 9, 1, 7, 5, 3 ) ), 'true', 'example 2' ); +is( arithmetic_progression( ( 1, 2, 4, 8, 16 ) ), 'false', 'example 3' ); +is( arithmetic_progression( ( 5, -1, 3, 1, -3 ) ), 'true', 'example 4' ); +is( arithmetic_progression( ( 1.5, 3, 0, 4.5, 6 ) ), 'true', 'example 5' );