Home > other >  calculate h-index in PHP
calculate h-index in PHP

Time:08-30

The h-index is a measure for a scientist's productivity and impact.

To cite the linked Wikipedia page:

The h-index is the largest number h such that h articles have at least h citations each. For example, if an author has five publications, with 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the author's h-index is 3, because the author has three publications with 3 or more citations.

How to calculate the h-index in PHP, given an array of citation counts per publication?

<?php

$citations = array(51, 27, 14, 14, 6, 2, 1, 0, 0, 0);

Here, the h-index would be 5 (if I'm not mistaken); but how to find this result in PHP?

CodePudding user response:

As the algo is explained in the wiki, I would go with this:

$hIndex = function(array $publications): int {
    rsort($publications);
    foreach ($publications as $index => $publication) {
        if($index >= $publication) {
            return $index;
        }
    }

    return 0;
};

echo $hIndex([9, 7, 6, 2, 1]), PHP_EOL;
echo $hIndex([51, 27, 14, 14, 6, 2, 1, 0, 0, 0]), PHP_EOL;
echo $hIndex([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]), PHP_EOL;
echo $hIndex([100, 100, 2, 2, 2, 2, 2, 2, 2, 2]), PHP_EOL;
echo $hIndex([100, 100, 9, 8, 3, 2, 2, 1, 1, 0]), PHP_EOL;

print

3
5
5
2
4
  • Related