From 113e4dfdd5adc942e5b467fffe5f54996b7aa4a3 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 21 Nov 2022 08:03:23 +0000 Subject: Create ch-2.pl --- challenge-192/james-smith/perl/ch-2.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-192/james-smith/perl/ch-2.pl diff --git a/challenge-192/james-smith/perl/ch-2.pl b/challenge-192/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..36b2dc27dc --- /dev/null +++ b/challenge-192/james-smith/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( [ [1,0,5], 4 ], [ [0,2,0],-1], [ [0,3,0], 2 ] ); + +is( equal_dis( @{$_->[0]} ), $_->[1] ) for @TESTS; +done_testing(); + +sub equal_dis { + my($av,$k) = (0,0); + $av+=$_ for @_; + return -1 if $av%@_; + $av/=@_; + $k+=abs($av-$_[0]),$_[1]-=$av-shift while @_>1; + $k; +} -- cgit