From 1cab2e22fbc8b2b203cd8d02b2b018ef9ee1a256 Mon Sep 17 00:00:00 2001 From: zimmersandro Date: Mon, 20 Apr 2026 18:03:24 +0200 Subject: [PATCH] feat: function which returns top three most used word in a text --- src/codewars/kata_most_words_used.py | 52 ++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/codewars/kata_most_words_used.py b/src/codewars/kata_most_words_used.py index 2b3451c..96e2f9c 100644 --- a/src/codewars/kata_most_words_used.py +++ b/src/codewars/kata_most_words_used.py @@ -1,2 +1,50 @@ -def top_3_words(text): - return None +def top_3_words(text: str) -> list: + """Given a string of text (possibly with punctuation and line-breaks), + returns an array of the top-3 most occurring words, in descending order + of the number of occurrences. + + Args: + text (str): Some text + + Returns: + list: [most used word, second most used word, third most used word] + """ + text = "".join( + c if c.isalpha() or c.isdigit() or c in ("'", " ") else " " for c in text + ) # remove invalid characters and replace with whitespace + text = text.split() + + word_count = {} + + for word in text: + if word in word_count: + word_count[word] += 1 + else: + word_count[word] = 1 + + top_3 = { + "first": {"word": None, "amount": 0}, + "second": {"word": None, "amount": 0}, + "third": {"word": None, "amount": 0}, + } + + for word in word_count: + if word_count[word] > top_3["first"]["amount"]: + top_3["first"]["word"] = word + top_3["first"]["amount"] = word_count[word] + elif word_count[word] > top_3["second"]["amount"]: + top_3["second"]["word"] = word + top_3["second"]["amount"] = word_count[word] + elif word_count[word] > top_3["third"]["amount"]: + top_3["third"]["word"] = word + top_3["third"]["amount"] = word_count[word] + + return [ + x + for x in [ + top_3["first"]["word"], + top_3["second"]["word"], + top_3["third"]["word"], + ] + if x is not None + ]