Word Counter
Word counting is useful in search, logs, text analysis, and even code parsing. Given a string text where words are separated by spaces, return a dictionary that maps each word to how many times it appears. For an empty string, return an empty dictionary.
Примеры
Ввод
"a b a c a b"Вывод
{"a": 3, "b": 2, "c": 1}Ввод
""Вывод
{}Ввод
"hello"Вывод
{"hello": 1}Нужно реализовать
def word_freq(text):
Теги
stringsdicthashmap
Zaloguj się, aby zobaczyć historię rozwiązań
Zaloguj sięZaloguj się, aby korzystać z Mentora AI
Zaloguj sięVisible tests
1
Input
"a b a c a b"Expected
{"a":3,"b":2,"c":1}2
Input
""Expected
{}3
Input
"hello"Expected
{"hello":1}4
Input
"x x x"Expected
{"x":3}5
Input
"the cat the dog"Expected
{"cat":1,"dog":1,"the":2}6
Input
"HELLO"Expected
{"HELLO":1}Focus radio
Paused · SomaFM · Fluid