From 53440f8e396e98abf4e042cd7f7ed3d7d048b1da Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Mon, 19 Dec 2022 11:11:59 +0400 Subject: [PATCH 1/4] add h-index algorigthm --- other/h_index.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 other/h_index.py diff --git a/other/h_index.py b/other/h_index.py new file mode 100644 index 000000000000..de599a71058c --- /dev/null +++ b/other/h_index.py @@ -0,0 +1,71 @@ +""" +Task: +Given an array of integers citations where citations[i] is the number of +citations a researcher received for their ith paper, return compute the +researcher's h-index. + +According to the definition of h-index on Wikipedia: A scientist has an +index h if h of their n papers have at least h citations each, and the other +n - h papers have no more than h citations each. + +If there are several possible values for h, the maximum one is taken as the +h-index. + +H-Index link: https://en.wikipedia.org/wiki/H-index + +Implementation notes: +Use sorting of array + +Leetcode link: https://leetcode.com/problems/h-index/description/ + +n = len(citations) +Runtime Complexity: O(n * log(n)) +Space Complexity: O(1) + +""" + +def h_index(citations: list[int]) -> int: + """ + 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_index([1,2,'3']) + Traceback (most recent call last): + ... + ValueError: The citations should be a list of non negative integers. + >>> h_index([1,2,-3]) + Traceback (most recent call last): + ... + ValueError: The citations should be a list of non negative integers. + """ + + # validate: + if not isinstance(citations, list) or \ + not all([isinstance(item, int) and item >= 0 for item in citations]): + raise ValueError( + 'The citations should be a list of non negative integers.' + ) + + citations.sort() + len_citations = len(citations) + + for i in range(len_citations): + if citations[len_citations - 1 - i] <= i: + return i + + return len_citations + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From d8d1497bd567c38005052df43830c37550dea950 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 19 Dec 2022 07:12:43 +0000 Subject: [PATCH 2/4] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 34ce88a4f2ab..f9f4a2ec113a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -695,6 +695,7 @@ * [Gauss Easter](other/gauss_easter.py) * [Graham Scan](other/graham_scan.py) * [Greedy](other/greedy.py) + * [H Index](other/h_index.py) * [Least Recently Used](other/least_recently_used.py) * [Lfu Cache](other/lfu_cache.py) * [Linear Congruential Generator](other/linear_congruential_generator.py) From b8fc4502f43b5fecbe2ddf65b1e59d41fb034428 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 19 Dec 2022 07:14:15 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- other/h_index.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/other/h_index.py b/other/h_index.py index de599a71058c..bab0b1147034 100644 --- a/other/h_index.py +++ b/other/h_index.py @@ -13,7 +13,7 @@ H-Index link: https://en.wikipedia.org/wiki/H-index -Implementation notes: +Implementation notes: Use sorting of array Leetcode link: https://leetcode.com/problems/h-index/description/ @@ -24,6 +24,7 @@ """ + def h_index(citations: list[int]) -> int: """ Return H-index of citations @@ -49,19 +50,18 @@ def h_index(citations: list[int]) -> int: """ # validate: - if not isinstance(citations, list) or \ - not all([isinstance(item, int) and item >= 0 for item in citations]): - raise ValueError( - 'The citations should be a list of non negative integers.' - ) + if not isinstance(citations, list) or not all( + [isinstance(item, int) and item >= 0 for item in citations] + ): + raise ValueError("The citations should be a list of non negative integers.") citations.sort() len_citations = len(citations) - + for i in range(len_citations): if citations[len_citations - 1 - i] <= i: return i - + return len_citations From 0b26c415b13980bc239b2c580212bca241de848d Mon Sep 17 00:00:00 2001 From: Alexander Pantyukhin Date: Thu, 5 Jan 2023 11:59:16 +0400 Subject: [PATCH 4/4] Update other/h_index.py Co-authored-by: Caeden Perelli-Harris --- other/h_index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/h_index.py b/other/h_index.py index bab0b1147034..e91389675b16 100644 --- a/other/h_index.py +++ b/other/h_index.py @@ -51,7 +51,7 @@ def h_index(citations: list[int]) -> int: # validate: if not isinstance(citations, list) or not all( - [isinstance(item, int) and item >= 0 for item in citations] + isinstance(item, int) and item >= 0 for item in citations ): raise ValueError("The citations should be a list of non negative integers.")