Word Length Frequency Report

Hard ⏱ 18 min 33% acceptance ★★★★☆ 4.3
Write length_report(text) that returns a dict mapping each distinct word length to how many words of that length appear in text (split on whitespace, strip punctuation .,!? from the ends of words, ignore empties). Use a dict comprehension over the set of lengths.

Examples

Example 1
Input
length_report('hi bye, no go!')
Output
{2: 3, 3: 1}
Explanation

Words are hi, bye, no, go → lengths 2,3,2,2.

Constraints

  • Strip only the characters .,!? from word ends.
  • No collections.Counter — build it with comprehensions.

Topics

List Comprehensionsdict comprehensionaggregation

Companies

GoogleAmazonGoldman Sachs

Hints

Hint 1

lengths = [len(w.strip('.,!?')) for w in text.split() if w.strip('.,!?')]

Hint 2

{n: lengths.count(n) for n in set(lengths)} finishes it.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.