From 2b99b6e28c2a3b672e50d7b22cd3e1c6998670c7 Mon Sep 17 00:00:00 2001 From: James Smith Date: Tue, 13 Dec 2022 05:05:01 +0000 Subject: Create ch-2.pl --- challenge-195/james-smith/perl/ch-2.pl | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 challenge-195/james-smith/perl/ch-2.pl diff --git a/challenge-195/james-smith/perl/ch-2.pl b/challenge-195/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..592ee325c4 --- /dev/null +++ b/challenge-195/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); + +my @TESTS = ( + [ [ 1,1,2,6,2 ] => 2 ], + [ [ 1,3,5,7 ] => -1 ], + [ [ 6,4,4,6,1 ] => 4 ], +); + +is( mf_even( @{$_->[0]} ), $_->[1] ) for @TESTS; +done_testing(); + +sub mf_even { + my($m,$v,%f)=(-1,-1); + $_%2 || $f{$_}++ for @_; ## Compute frequencies + ( $f{$_}>$v || $f{$_}==$v && $_<$m ) && ( $m=$_,$v=$f{$_} ) for keys %f; + $m +} -- cgit