Classify values in one pass

Problem

Implement classify_values(xs). Return a dictionary with keys negative, zero, positive_even, and positive_odd. Each value is the count of integers in xs in that category. Use one pass over xs.

Starter code

def classify_values(xs):
    pass
Reveal answer or reference solution
def classify_values(xs):
    out = {"negative": 0, "zero": 0, "positive_even": 0, "positive_odd": 0}
    for x in xs:
        if x < 0:
            out["negative"] += 1
        elif x == 0:
            out["zero"] += 1
        elif x % 2 == 0:
            out["positive_even"] += 1
        else:
            out["positive_odd"] += 1
    return out

Public tests

  • classify_values([-2, 0, 1, 2, 3, 4]){'negative': 1, 'zero': 1, 'positive_even': 2, 'positive_odd': 2}
  • classify_values([]){'negative': 0, 'zero': 0, 'positive_even': 0, 'positive_odd': 0}

Local history

Loading attempts saved in this browser…

Use with your agent

Share this URL and your attempt. Ask the agent to start with a clarifying question or the smallest useful hint.

Tutor me on https://mlprep.iwase.dev/programming/diagnostic/original-py-control-flow/. If window.mlPrepAgent is available, read attempts for item original-py-control-flow before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record and read it back.

Appears in