Return H-index of citations >>> h_index([3, 0, 6, 1, 5]) 3 >>> h_index([1, 3, 1]) 1 >>> h_index([1, 2, 3]) 2 >>> h_index('test') Traceback (most recent call last): ... ValueError: The citations should be a list of non negative integers. >>> h_ind
(citations: list[int])
| 26 | |
| 27 | |
| 28 | def h_index(citations: list[int]) -> int: |
| 29 | """ |
| 30 | Return H-index of citations |
| 31 | |
| 32 | >>> h_index([3, 0, 6, 1, 5]) |
| 33 | 3 |
| 34 | >>> h_index([1, 3, 1]) |
| 35 | 1 |
| 36 | >>> h_index([1, 2, 3]) |
| 37 | 2 |
| 38 | >>> h_index('test') |
| 39 | Traceback (most recent call last): |
| 40 | ... |
| 41 | ValueError: The citations should be a list of non negative integers. |
| 42 | >>> h_index([1,2,'3']) |
| 43 | Traceback (most recent call last): |
| 44 | ... |
| 45 | ValueError: The citations should be a list of non negative integers. |
| 46 | >>> h_index([1,2,-3]) |
| 47 | Traceback (most recent call last): |
| 48 | ... |
| 49 | ValueError: The citations should be a list of non negative integers. |
| 50 | """ |
| 51 | |
| 52 | # validate: |
| 53 | if not isinstance(citations, list) or not all( |
| 54 | isinstance(item, int) and item >= 0 for item in citations |
| 55 | ): |
| 56 | raise ValueError("The citations should be a list of non negative integers.") |
| 57 | |
| 58 | citations.sort() |
| 59 | len_citations = len(citations) |
| 60 | |
| 61 | for i in range(len_citations): |
| 62 | if citations[len_citations - 1 - i] <= i: |
| 63 | return i |
| 64 | |
| 65 | return len_citations |
| 66 | |
| 67 | |
| 68 | if __name__ == "__main__": |