202. Python Program to Count Positive, Negative and Zero Numbers in a List
EasyPhase 1 · Warm-up
This one uses split(), a for loop and if, elif and else. You know them all.
Overview
Putting numbers into groups and counting each group is a common interview warm-up. It is the same idea that reports and charts use.
Problem Statement
Noah writes down the temperature every morning, in whole degrees. Some mornings are above 0, some are below 0, and some are exactly 0.
Read the temperatures. Count how many are positive (more than 0), how many are negative (less than 0) and how many are zero.
Steps
- Read the line and split it into a list.
- Make three counters that start at 0.
- Loop over the list. Turn each piece into a number, and add 1 to the right counter.
- Print the three counts, one per line, in the order shown below.
Examples
Example 1. Input 3 -1 0 7 -5 gives:
Positive: 2
Negative: 2
Zero: 1
Why: 3 and 7 are more than 0, -1 and -5 are less than 0, and there is one 0.
Example 2. Input 0 0 0 gives:
Positive: 0
Negative: 0
Zero: 3
Why: 0 is neither positive nor negative, so all three numbers count as zero.
Input
One line of whole numbers, with one space between them (1 number or more). Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
Three lines, in this order:
Positive: <count>
Negative: <count>
Zero: <count>
The tests try 5 different inputs.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.