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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import List
def compute_h_index(citations: List[int]) -> int:
"""
Computes the H-Index for a researcher given an array of their citations.
The H-Index is the largest number h such that h articles have at least h citations each.
Args:
citations: A list of integers representing the citations of a researcher's publications.
Returns:
The H-Index of the researcher.
Examples:
>>> compute_h_index([10, 8, 5, 4, 3])
4
>>> compute_h_index([25, 8, 5, 3, 3])
3
"""
# Sort the citations in descending order
citations = sorted(citations, reverse=True)
# Find the largest h such that h articles have at least h citations each
h_index = 0
for i, citation in enumerate(citations):
h = i + 1
if citation >= h:
h_index = h
else:
break
return h_index
# Tests
def test_compute_h_index():
assert compute_h_index([10, 8, 5, 4, 3]) == 4
assert compute_h_index([25, 8, 5, 3, 3]) == 3
if __name__ == '__main__':
test_compute_h_index()
|