# Python: Find the largest negative and smallest positive numbers

## Python Programming Puzzles: Exercise-79 with Solution

Write a Python program to find the largest negative and smallest positive numbers (or 0 if none).

Input: [-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18] Output: [-6, 2] Input: [-1, -2, -3, -4] Output: [-1, 0] Input: [1, 2, 3, 4] Output: [0, 1] Input: [] Output: [0, 0]

**Pictorial Presentation:**

**Sample Solution:**

**Python Code:**

```
#License: https://bit.ly/3oLErEI
def test(nums):
pos = [n for n in nums if n > 0]
neg = [n for n in nums if n < 0]
return [max(neg) if neg else 0, min(pos) if pos else 0]
nums=[-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18]
print("List of numbers:",nums)
print("Largest negative and smallest positive numbers (or 0 if none) of the said list:")
print(test(nums))
nums=[-1, -2, -3, -4]
print("\nList of numbers:",nums)
print("Largest negative and smallest positive numbers (or 0 if none) of the said list:")
print(test(nums))
nums=[1, 2, 3, 4]
print("\nList of numbers:",nums)
print("Largest negative and smallest positive numbers (or 0 if none) of the said list:")
print(test(nums))
nums=[]
print("\nList of numbers:",nums)
print("Largest negative and smallest positive numbers (or 0 if none) of the said list:")
print(test(nums))
```

Sample Output:

List of numbers: [-12, -6, 300, -40, 2, 2, 3, 57, -50, -22, 12, 40, 9, 11, 18] Largest negative and smallest positive numbers (or 0 if none) of the said list: [-6, 2] List of numbers: [-1, -2, -3, -4] Largest negative and smallest positive numbers (or 0 if none) of the said list: [-1, 0] List of numbers: [1, 2, 3, 4] Largest negative and smallest positive numbers (or 0 if none) of the said list: [0, 1] List of numbers: [] Largest negative and smallest positive numbers (or 0 if none) of the said list: [0, 0]

**Flowchart:**

**Python Code Editor :**

**Have another way to solve this solution? Contribute your code (and comments) through Disqus.**

**Previous:** Find the two closest distinct numbers in a given a list of numbers.

**Next:** Round each float in a list of numbers up to the next integer and return the running total of the integer squares.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

**Weekly Trends**- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join