From 16e17609fb5fcdc9e7f09284bc8fbbca56b268cf Mon Sep 17 00:00:00 2001 From: James Smith Date: Sun, 12 Mar 2023 08:43:51 +0000 Subject: Create ch-2.pl --- challenge-207/james-smith/perl/ch-2.pl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-207/james-smith/perl/ch-2.pl diff --git a/challenge-207/james-smith/perl/ch-2.pl b/challenge-207/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..bc06c9c57d --- /dev/null +++ b/challenge-207/james-smith/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; + +my @TESTS = ( + [ [10,8,5,4,3], 4 ], + [ [25,8,5,3,3], 3 ], +); + +is( h_index( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( h_index_2( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( h_index_3( @{$_->[0]} ), $_->[1] ) for @TESTS; + +done_testing(); + +sub h_index { ( $_[$_]>$_) && return $_+1 for reverse 0..$#_ } +sub h_index_2 { pop @_ while $_[-1] < @_; 0 + @_ } +sub h_index_3 { ( $_[$_]>$_) || return $_ for 0..$#_; 0+@_ } -- cgit