Walrus Operator While Filtering

Hard ⏱ 18 min 28% acceptance ★★★★★ 4.6
Write a function count_long_words(words, min_length) that counts how many strings in list words, after stripping surrounding whitespace, have length >= min_length. Implement the counting with a list comprehension that uses the walrus operator := to both compute the stripped word and test its length in the same expression, avoiding calling .strip() twice per word.

Examples

Example 1
Input
count_long_words([" hi ", "hello", " world "], 5)
Output
2
Explanation

"hello" and "world" (after stripping) both have length 5.

Example 2
Input
count_long_words([], 3)
Output
0
Explanation

No words at all.

Constraints

  • words is a list of strings; min_length is a non-negative int.

Topics

Operatorsassignment expressions

Companies

GoogleMetaAtlassian

Hints

Hint 1

Inside the comprehension condition: `if (stripped := w.strip()) and len(stripped) >= min_length` — but here stripped is never falsy-empty relevant, so simply `if len((s := w.strip())) >= min_length`.

Hint 2

The walrus lets you bind s = w.strip() right inside the filter condition, avoiding a second .strip() call when building the kept list.

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