Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions challenge-351/matt-martini/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Solution by Matt Martini
45 changes: 45 additions & 0 deletions challenge-351/matt-martini/perl/ch-1.pl
Original file line number Diff line number Diff line change
@@ -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' );
41 changes: 41 additions & 0 deletions challenge-351/matt-martini/perl/ch-2.pl
Original file line number Diff line number Diff line change
@@ -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' );