CJCoding With Joseph
15free / daySign inCreate account

Count Frequency With A Dict

Write count_frequency(words: list[str]) -> dict[str, int] that returns a dictionary mapping each distinct word to how many times it appears. Loop over the words once: when the word is already a key, add one to its value, otherwise create the key with a value of 1. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: one line of space-separated words; the tests print each word and its count on one line, separated by a space, in alphabetical order of the words.

Expected Output:

blue 2
green 1
red 3

= newline. The output ends with a newline — print one after the last line.

Hints3 available
Topics:
Data Structures & Algorithms
📥 Auto-Input:
red blue red green blue red\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.