| 21 | |
| 22 | @total_ordering |
| 23 | class WordCount: |
| 24 | def __init__(self, word: str, count: int) -> None: |
| 25 | self.word = word |
| 26 | self.count = count |
| 27 | |
| 28 | def __eq__(self, other: object) -> bool: |
| 29 | """ |
| 30 | >>> WordCount('a', 1).__eq__(WordCount('b', 1)) |
| 31 | True |
| 32 | >>> WordCount('a', 1).__eq__(WordCount('a', 1)) |
| 33 | True |
| 34 | >>> WordCount('a', 1).__eq__(WordCount('a', 2)) |
| 35 | False |
| 36 | >>> WordCount('a', 1).__eq__(WordCount('b', 2)) |
| 37 | False |
| 38 | >>> WordCount('a', 1).__eq__(1) |
| 39 | NotImplemented |
| 40 | """ |
| 41 | if not isinstance(other, WordCount): |
| 42 | return NotImplemented |
| 43 | return self.count == other.count |
| 44 | |
| 45 | def __lt__(self, other: object) -> bool: |
| 46 | """ |
| 47 | >>> WordCount('a', 1).__lt__(WordCount('b', 1)) |
| 48 | False |
| 49 | >>> WordCount('a', 1).__lt__(WordCount('a', 1)) |
| 50 | False |
| 51 | >>> WordCount('a', 1).__lt__(WordCount('a', 2)) |
| 52 | True |
| 53 | >>> WordCount('a', 1).__lt__(WordCount('b', 2)) |
| 54 | True |
| 55 | >>> WordCount('a', 2).__lt__(WordCount('a', 1)) |
| 56 | False |
| 57 | >>> WordCount('a', 2).__lt__(WordCount('b', 1)) |
| 58 | False |
| 59 | >>> WordCount('a', 1).__lt__(1) |
| 60 | NotImplemented |
| 61 | """ |
| 62 | if not isinstance(other, WordCount): |
| 63 | return NotImplemented |
| 64 | return self.count < other.count |
| 65 | |
| 66 | |
| 67 | def top_k_frequent_words(words: list[str], k_value: int) -> list[str]: |
no outgoing calls
no test coverage detected