From d9393152c59ff7c2eb4a4cc28f124b0f7ed5b8b2 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 25 Oct 2023 19:18:05 -0400 Subject: [PATCH] Something like h-index? --- leetcode/stuff.py | 36 ++++++++++++++++++++++++++++++++++++ leetcode/test_stuff.py | 21 +++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/leetcode/stuff.py b/leetcode/stuff.py index c90fa82..d786145 100644 --- a/leetcode/stuff.py +++ b/leetcode/stuff.py @@ -381,3 +381,39 @@ def collect_min_jumps_from_board(board: list[int]) -> list[int]: range_end = range_begin + val + 1 return jumps + [len(board) - 1] + + +def collect_scored_citations(citations: list[int]) -> list[int]: + return [ + i for i, v in enumerate(list(sorted(citations, reverse=True))) if v >= (i + 1) + ] + + +def collect_h_index_qualified_citations(citations: list[int]) -> set[int]: + census: dict[int, int] = collect_h_index_census_from_citations(citations) + return {k for k, v in census.items() if v >= k} + + +def collect_h_index_census_from_citations(citations: list[int]) -> dict[int, int]: + census: dict[int, int] = {} + + for n in set(citations): + census.setdefault(n, 0) + + for paper in citations: + if paper >= n: + census[n] += 1 + + return census + + +def h_index(citations: list[int]) -> int: + last_qualified = None + + for i, citation in enumerate(list(sorted(citations, reverse=True))): + if citation >= i + 1: + last_qualified = i + 1 + else: + break + + return last_qualified or 0 diff --git a/leetcode/test_stuff.py b/leetcode/test_stuff.py index 53603ca..3ba42a3 100644 --- a/leetcode/test_stuff.py +++ b/leetcode/test_stuff.py @@ -310,3 +310,24 @@ def test_collect_complete_jump_paths_from_board( ) def test_count_min_jumps_from_board(board: list[int], expected: int): assert stuff.count_min_jumps_from_board(board) == expected + + +@pytest.mark.parametrize( + ("citations", "expected"), + [ + ( + [3, 0, 6, 1, 5], + 3, + ), + ( + [1, 3, 1], + 1, + ), + ( + [100], + 1, + ), + ], +) +def test_h_index(citations: list[int], expected: int): + assert stuff.h_index(citations) == expected