Pass/Fail Labels in One Line

Easy ⏱ 8 min 87% acceptance ★★★★★ 4.7
Write label_scores(scores, cutoff) returning a list where each score is replaced by the string "pass" if it is at least cutoff, else "fail". Note the difference from filtering: here the if/else sits in the expression position, so every score produces an output.

Examples

Example 1
Input
label_scores([35, 80, 50], 40)
Output
['fail', 'pass', 'pass']
Explanation

35 is below 40; the others meet the cutoff.

Constraints

  • Output length equals input length.
  • Use a single comprehension.

Topics

List Comprehensionsconditional expression

Companies

AccentureCognizant

Hints

Hint 1

The pattern is [A if cond else B for x in xs].

Hint 2

No trailing if-filter here.

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