2.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69e2383af7832c8032603b91 | Challenge 275: Character Frequency | 29 | challenge-275 |
--description--
Given a string, return an object (JavaScript) or dictionary (Python) mapping each character to the number of times it appears.
--hints--
get_frequency("test") should return {"t": 2, "e": 1, "s": 1}.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_frequency("test"), {"t": 2, "e": 1, "s": 1})`)
}})
get_frequency("mississippi") should return {"m": 1, "i": 4, "s": 4, "p": 2}.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_frequency("mississippi"), {"m": 1, "i": 4, "s": 4, "p": 2})`)
}})
get_frequency("hello world") should return {"h": 1, "e": 1, "l": 3, "o": 2, " ": 1, "w": 1, "r": 1, "d": 1}.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_frequency("hello world"), {"h": 1, "e": 1, "l": 3, "o": 2, " ": 1, "w": 1, "r": 1, "d": 1})`)
}})
get_frequency("She sells seashells by the seashore.") should return {"S": 1, "h": 4, "e": 7, " ": 5, "s": 7, "l": 4, "a": 2, "b": 1, "y": 1, "t": 1, "o": 1, "r": 1, ".": 1}.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_frequency("She sells seashells by the seashore."), {"S": 1, "h": 4, "e": 7, " ": 5, "s": 7, "l": 4, "a": 2, "b": 1, "y": 1, "t": 1, "o": 1, "r": 1, ".": 1})`)
}})
get_frequency("The quick brown fox jumps over the lazy dog.") should return {"T": 1, "h": 2, "e": 3, " ": 8, "q": 1, "u": 2, "i": 1, "c": 1, "k": 1, "b": 1, "r": 2, "o": 4, "w": 1, "n": 1, "f": 1, "x": 1, "j": 1, "m": 1, "p": 1, "s": 1, "v": 1, "t": 1, "l": 1, "a": 1, "z": 1, "y": 1, "d": 1, "g": 1, ".": 1}.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_frequency("The quick brown fox jumps over the lazy dog."), {"T": 1, "h": 2, "e": 3, " ": 8, "q": 1, "u": 2, "i": 1, "c": 1, "k": 1, "b": 1, "r": 2, "o": 4, "w": 1, "n": 1, "f": 1, "x": 1, "j": 1, "m": 1, "p": 1, "s": 1, "v": 1, "t": 1, "l": 1, "a": 1, "z": 1, "y": 1, "d": 1, "g": 1, ".": 1})`)
}})
--seed--
--seed-contents--
def get_frequency(s):
return s
--solutions--
def get_frequency(s):
freq = {}
for char in s:
freq[char] = freq.get(char, 0) + 1
return freq