Editor

Count a Word

#504 · Strings · Beginner · Review the AI's code · 25 XP · about 3 min

Someone asked an AI assistant: "Write a function that counts how many times a word appears in a sentence, ignoring upper and lower case." This is its answer. It looks right, but it has a bug.

count_word(sentence, word) should return how many words in sentence are equal to word when case is ignored, so 'The', 'the' and 'THE' all count as the same word. The sentence has only letters and spaces, and word is a single word.

Examples

count_word('The cat saw the dog', 'the')  # returns 2
count_word('The cat saw the dog', 'THE')  # returns 2
count_word('Go go GO', 'go')              # returns 3
count_word('A big dog', 'cat')            # returns 0

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.