aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/dave-jacoby/perl/ch-2.pl
blob: 3d7af432e18d478bf64a0cb07b392babec8ada7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ say postderef signatures state };

use List::Util qw{ max };

my @examples = (

    [ 10, 8, 5, 4, 3 ],
    [ 25, 8, 5, 3, 3 ],
);

for my $e (@examples) {
    my $o     = h_index( $e->@* );
    my $array = join ', ', $e->@*;
    say <<"END";
    Input:  \@array = $array
    Output: $o
END
}

sub h_index ( @citations ) {
    my $max = max @citations;
    for my $h ( 1 .. $max ) {
        my $i = () = grep { $_ >= $h } @citations;
        return $h - 1 if $i < $h;
    }
    return 0;
}