The Letter Ledger
A easy Python interview practice problem on DataDriven. Write and execute real python code with instant grading.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L4
Problem
Implement a `CharFrequency` class. Its constructor takes a string `text` and builds a frequency map of its characters, ignoring spaces. Its `get_counts()` method returns a list of `[char, count]` pairs (each a two-element list) for the distinct characters, sorted alphabetically by character. The test harness calls the provided `char_frequency(text)` driver, which constructs `CharFrequency(text)` and returns `char_frequency.get_counts()`. For `"hello world"` it returns `[["d", 1], ["e", 1], ["h", 1], ["l", 3], ["o", 2], ["r", 1], ["w", 1]]`. An empty (or all-spaces) string yields an empty list.
Summary
Every character has a count to answer for.
Practice This Problem
Solve this Python problem with real code execution. DataDriven runs your Python code in a real environment and grades it automatically.