Read a list of whole numbers and print the largest and the smallest.
What to build
A program that reads a list and reports its extremes.
Requirements
- Read the numbers into a list.
- Print the largest and then the smallest on one line.
Input
The first line has n (1 to 1000). The second line has n whole numbers separated by spaces.
Output
Two numbers on one line: the largest, then the smallest.
Constraints
n is from 1 to 1000.
Example
Input:
5
3 9 -2 7 4
Output:
9 -2
Why it works
max and min go through the whole list for you, and work for negative numbers too.
Examples
Example 1 · Input
5 3 9 -2 7 4
Expected output
9 -2
Example 2 · Input
1 42
Expected output
42 42
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
max() and min() work on a whole list.
Solution
Show one correct solution (try it yourself first)
n = int(input()) nums = [int(x) for x in input().split()] print(max(nums), min(nums))