PracticeArrays

Count Negative Audit Scores

BeginnerArraystraversalaccumulationcomparison Time not estimated Not started

Context

A document-processing service assigns an integer sentiment score to each recorded entry in an audit log. Scores below zero indicate an unfavorable entry. Create a small analysis function that reports how many recorded scores are unfavorable, without changing the log order or storing another collection.

Problem

Given an integer array of audit-log scores, return the number of elements whose value is strictly less than zero. Count every qualifying occurrence, including repeated values. A score of zero is not negative, and positive scores are not counted. If the array is empty, return zero.

Examples

Example 1
Input: scores = [12, -4, 0, -4, 7, -1]
Output: 3
Explanation: For [12, -4, 0, -4, 7, -1], the executed result is 3. Three negative occurrences are counted, including both occurrences of -4, while zero and positive values are not counted.
Example 2
Input: scores = [-9, -2, -15, -2]
Output: 4
Explanation: For [-9, -2, -15, -2], the executed result is 4. Each of the four negative occurrences is counted, including the repeated -2 values.
Example 3
Input: scores = []
Output: 0
Explanation: For [], the executed result is 0. An empty array returns zero.

Constraints

  • 0 <= len(scores) <= 100000
  • -1000000000 <= scores[i] <= 1000000000 for every i
  • Types: scores is int[]; result is int

Function signature

def count_negative_scores(scores: list[int]) -> int
scores list[int]
The ordered integer scores recorded for document-log entries.
returns int
The number of scores strictly less than zero.

Adapted from MBPP problem task_329 (CC BY 4.0). Rewritten, extended and verified by Iksha.

Approved · c012r1-abs_783_v414 execution-verified tests
Code
Saved
Checking environment…Python SandboxLn 1, Col 1