aboutsummaryrefslogtreecommitdiff
path: root/challenge-207/jo-37/perl/ch-2.pl
blob: 7f84bd9edfd06962960f6d25089388927f985a98 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/perl -s

use v5.16;
use Test2::V0 '!float';
use PDL;
use PDL::NiceSlice;

our ($tests, $examples);

run_tests() if $tests || $examples;	# does not return

die <<EOS unless @ARGV;
usage: $0 [-examples] [-tests] [N...]

-examples
    run the examples from the challenge
 
-tests
    run some tests

N...
    calculate the H-Index for the given numbers

EOS


### Input and Output

say hirsch(@ARGV);


### Implementation

sub hirsch {
    # Compare the reverse-sorted list's elements with their index
    # position and pick the largest index where the difference is
    # positive.  Finally adjust the zero-based array index to the
    # one-based H-Index.
    which(pdl(@_)->qsort->(-1:0) > sequence(scalar @_))->((-1)) + 1;
}


### Examples and tests

sub run_tests {
    SKIP: {
        skip "examples" unless $examples;

        is hirsch(10, 8, 5, 4, 3), 4, 'example 1';
        is hirsch(25, 8, 5, 3, 3), 3, 'example 2';
    }

    SKIP: {
        skip "tests" unless $tests;

        is hirsch(9, 7, 6, 2, 1), 3, 'from Wikipedia';
        is hirsch(1, 7, 6, 2, 9), 3, 'same, not sorted';
        is hirsch(5, 5, 5, 5, 5), 5, 'last index';
	}

    done_testing;
    exit;
}