easy +10 pts

Count word occurrences

Count how many times each word appears in a sentence.

Write a function `count_words(sentence: str) -> dict` that takes a string `sentence` and returns a dictionary where keys are words (lowercased) and values are the number of times each word appears. A word is defined as a maximal sequence of letters (A-Z, a-z). Hyphens, apostrophes, digits, and punctuation do not count as part of a word. For example, in `"don't"`, the word is `"don"` and `"t"` separately? Actually, according to our rule, the apostrophe is not a letter, so `"don't"` would split into `"don"` and `"t"`. But for simplicity in this challenge, we will treat any non-letter character as a separator. So `"it's"` becomes `"it"` and `"s"`. If there are no words in the sentence, return an empty dictionary. The order of keys in the dictionary does not matter for equality. Examples: ```python >>> count_words("Hello world hello") {'hello': 2, 'world': 1} >>> count_words("Python is fun. Python is powerful!") {'python': 2, 'is': 2, 'fun': 1, 'powerful': 1} >>> count_words("One, two, one!") {'one': 2, 'two': 1} >>> count_words(" ") {} ```

Constraints

- `0 <= len(sentence) <= 10^5` - Words are case-insensitive: convert to lowercase. - Only letters (A-Z, a-z) are considered part of a word; all other characters are separators. - Return a dictionary with integer counts.

Example

```python
>>> count_words("Hello world hello")
{'hello': 2, 'world': 1}
>>> count_words("Python is fun. Python is powerful!")
{'python': 2, 'is': 2, 'fun': 1, 'powerful': 1}
>>> count_words("One, two, one!")
{'one': 2, 'two': 1}
>>> count_words("   ") 
{}
```
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think about extracting words by splitting on non-letter characters. You can use a loop or a list comprehension with a condition.
Use a dictionary to accumulate counts: for each word, increment its value.
Remember to lowercase each word before counting.
If the sentence contains no letters, return an empty dictionary.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.