← Back to Data Structures & Algorithms
Question 539
Count Frequency With A Dict
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
Sign in to use hintsTopics:
Data Structures & Algorithms
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.