Print Positive Numbers From Array in Python (Time Complexity: O(n))


Given an array of integers, print to the console the positive numbers, one on each line.

A number n is positive if it is strictly greater than 0.

Example:

Input: nums = [3, -2, 0, 4, -4]
Output (console):
3
4

Note:

You have to print the numbers to the console, your function shouldn't return anything.


Understanding the Problem

The core challenge of this problem is to iterate through the given array and identify the positive numbers. The significance of this problem lies in its simplicity and its frequent use in filtering data. A common pitfall is to include zero or negative numbers, which should be avoided.

Approach

To solve this problem, we can use a straightforward approach:

This approach is efficient with a time complexity of O(n), where n is the number of elements in the array.

Naive Solution

The naive solution involves iterating through the array and printing each positive number. This is already optimal for this problem since we only need to traverse the array once.

Optimized Solution

Given the simplicity of the problem, the naive solution is already optimal. There are no further optimizations needed.

Algorithm

Here is a step-by-step breakdown of the algorithm:

  1. Start iterating through the array from the first element to the last.
  2. For each element, check if it is greater than zero.
  3. If the condition is true, print the element.

Code Implementation

def print_positive_numbers(nums):
    # Iterate through each number in the array
    for num in nums:
        # Check if the number is positive
        if num > 0:
            # Print the positive number
            print(num)

# Example usage
nums = [3, -2, 0, 4, -4]
print_positive_numbers(nums)

Complexity Analysis

The time complexity of this approach is O(n), where n is the number of elements in the array. This is because we are iterating through the array once. The space complexity is O(1) as we are not using any additional space that scales with the input size.

Edge Cases

Consider the following edge cases:

Examples:

Input: nums = []
Output: (nothing)

Input: nums = [-1, -2, -3]
Output: (nothing)

Input: nums = [1, 2, 3]
Output:
1
2
3

Testing

To test the solution comprehensively, consider the following test cases:

Example test cases:

def test_print_positive_numbers():
    import io
    import sys

    # Helper function to capture print output
    def capture_output(func, *args, **kwargs):
        old_stdout = sys.stdout
        new_stdout = io.StringIO()
        sys.stdout = new_stdout
        func(*args, **kwargs)
        sys.stdout = old_stdout
        return new_stdout.getvalue().strip()

    # Test cases
    assert capture_output(print_positive_numbers, [3, -2, 0, 4, -4]) == "3\n4"
    assert capture_output(print_positive_numbers, []) == ""
    assert capture_output(print_positive_numbers, [-1, -2, -3]) == ""
    assert capture_output(print_positive_numbers, [1, 2, 3]) == "1\n2\n3"

    print("All tests passed.")

# Run tests
test_print_positive_numbers()

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to print positive numbers from an array in Python. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for developing strong problem-solving skills. Keep practicing and exploring further to improve your coding abilities.

Additional Resources

For further reading and practice, consider the following resources: