Word Frequency Counter

PYTHON coding challenge · Difficulty: easy · +50 XP

Function Signature

------------------

def word_frequency(text: str) -> dict:

Problem

-------

Given a string of text, count how many

times each word appears.

Rules:

• Convert all words to lowercase

• Ignore punctuation: . , ! ? ; : ' "

• Split on whitespace

• Return a dict: {word: count}

• Sort by count DESC, then word ASC

Example 1

---------

Input:

"Hello world hello Python world world"

Output:

{"world": 3, "hello": 2, "python": 1}

Example 2

---------

Input:

"Data, data! DATA is great. is it?"

Output:

{"data": 3, "is": 2, "great": 1,

"it": 1}

Example 3 (Edge case)

---------------------

Input: ""

Output: {}

Constraints

-----------

• 0 <= len(text) <= 10,000

• Words contain only letters after

punctuation is removed

• Return empty dict for empty string

Solve this challenge on PySpark.in