Reasoning Into CodeArunank Divya Sharan · Python edition← Arunank’s portfolio
Module 01 · Execution Grammar and State Mutation

004. Count Positives, Negatives, and Zeros

Foundation · Download this solution · All solutions and shared helpers

Problem

Count how many elements of an integer list are positive, negative and zero. Return a tuple in exactly that order. Each occurrence counts, including repeated values. The empty list returns (0, 0, 0), and the input is not changed.

Examples

Example 1

sign_counts([-2, 0, 4, -1, 0])
=> (1, 2, 2)

The tuple order is positive, negative, zero.

Example 2

sign_counts([])
=> (0, 0, 0)

No category has an occurrence.

Example 3

sign_counts([0, 0])
=> (0, 0, 2)

Repeated zeros each contribute.

Reveal the worked solution and reasoning

Why this mechanism works

The three categories partition the input: every integer belongs to one category and cannot belong to two. Express that exclusivity in the branch structure. The sum of the counters provides a cheap consistency check after every processed prefix.

State and boundaries

positive, negative and zero count their respective categories among consumed values.

Protocol

  1. Initialize all counters to zero.
  2. For each value choose exactly one sign branch.
  3. Increment only that branch's counter.
  4. Return the counters in the specified order.

Visualization

incoming value
  > 0 -> positive
  < 0 -> negative
  else -> zero

Python implementation

def sign_counts(nums):
    positive = negative = zero = 0
    for value in nums:
        if value > 0:
            positive += 1
        elif value < 0:
            negative += 1
        else:
            zero += 1
    return positive, negative, zero

Worked execution

Recorded state changes after statements; L numbers refer to the solution file. Long traces are explicitly condensed.

sign_counts([-2, 0, 4, -1, 0])

L2 sign_counts: positive = negative = zero = 0
nums=[-2, 0, 4, -1, 0]; positive=0; negative=0; zero=0

L3 sign_counts: for value in nums:
nums=[-2, 0, 4, -1, 0]; positive=0; negative=0; zero=0; value=-2

L7 sign_counts: negative += 1
nums=[-2, 0, 4, -1, 0]; positive=0; negative=1; zero=0; value=-2

L3 sign_counts: for value in nums:
nums=[-2, 0, 4, -1, 0]; positive=0; negative=1; zero=0; value=0

L9 sign_counts: zero += 1
nums=[-2, 0, 4, -1, 0]; positive=0; negative=1; zero=1; value=0

L3 sign_counts: for value in nums:
nums=[-2, 0, 4, -1, 0]; positive=0; negative=1; zero=1; value=4

L5 sign_counts: positive += 1
nums=[-2, 0, 4, -1, 0]; positive=1; negative=1; zero=1; value=4

L3 sign_counts: for value in nums:
nums=[-2, 0, 4, -1, 0]; positive=1; negative=1; zero=1; value=-1

L7 sign_counts: negative += 1
nums=[-2, 0, 4, -1, 0]; positive=1; negative=2; zero=1; value=-1

L3 sign_counts: for value in nums:
nums=[-2, 0, 4, -1, 0]; positive=1; negative=2; zero=1; value=0

L9 sign_counts: zero += 1
nums=[-2, 0, 4, -1, 0]; positive=1; negative=2; zero=2; value=0

Result: (1, 2, 2). The tuple order is positive, negative, zero.

sign_counts([])

L2 sign_counts: positive = negative = zero = 0
nums=[]; positive=0; negative=0; zero=0

Result: (0, 0, 0). No category has an occurrence.

Why the solution is correct

Initially all three counts match the empty prefix. Exactly one increment accounts for each new integer, so no occurrence is missed or counted twice. At the end the counters cover the entire list and sum to its length.

Time and space

O(n) time and O(1) auxiliary space.

Common mistakes

Three unrelated if statements are easy to make overlapping. A truthiness check classifies nonzero values together and loses their signs.

Reasoning card

State: positive, negative and zero count their respective categories among consumed values.

Invariant: Each processed value contributes to exactly one counter.

Decision to rehearse: Use mutually exclusive if/elif/else branches.

Finish: Return the counters in the specified order

Transfer exercise

Classify values into disjoint numeric bands, and prove that the boundary values belong to exactly one band.