| [Home](../index.html) | [Practices](../practices/index.html) | [Code](../code/index.html) | [Examples](../examples/index.html)

## The Problem

Given a string of text and a list of words, return the number of times each word appears in the text.

For example, if `text = "The quick brown fox jumps over the lazy dog"` and `words = ["the", "quick", "brown", "fox", "jumps", "over", "lazy", "dog"]`, then the function should return:

```

{

"the": 1,

"quick": 1,

"brown": 1,

"fox": 1,

"jumps": 1,

"over": 1,

"lazy": 1,

"dog": 1

}

```

## The Approach

One way to approach this problem is to iterate over each word in the `words` list and count how many times it appears in the `text` string. We can do this by iterating over each character in the `text` string, checking if the current character matches the current word we're counting, and incrementing our count if it does.

## The Implementation

We can implement this approach as follows:

```python

from typing import List

def word_count(text: str, words: List[str]) -> dict:

word_counts = {}

for word in words:

count = 0

for char in text:

if char.lower() == word.lower():

count += 1

word_counts[word] = count

return word_counts

```

Reply to this note

Please Login to reply.

Discussion

No replies yet.