Word Frequency Counter
Given a sentence, count how many times each word appears, using a dictionary.
Approach: split the sentence into words, then loop through them, using dict.get(word, 0) + 1 to increase each word's count — building up one dictionary of word -> count.
Input: One line: a sentence of space-separated words.
Output: One line: a Python dictionary mapping each word to how many times it appeared, in order of first appearance.
the cat sat on the mat the cat ran
{'the': 3, 'cat': 2, 'sat': 1, 'on': 1, 'mat': 1, 'ran': 1}- 1 <= number of words <= 1000
Hint 1
.split() with no arguments breaks the sentence into a list of words on whitespace.
Hint 2
freq.get(word, 0) safely returns 0 for a word not seen yet, so freq[word] = freq.get(word, 0) + 1 works for both new and repeated words.
Hint 3
Python dictionaries keep insertion order, so the printed dict will list words in the order they first appeared.
Split the sentence into words, then loop through them updating a dictionary with freq[w] = freq.get(w, 0) + 1 for each word — .get() avoids a KeyError the first time a word appears. Since dictionaries preserve insertion order, printing freq at the end naturally lists words in the order they were first seen.