Saturday, February 9, 2019

Min Max Example

Q. Given a list of numbers, find maximum and minimum in this list.

Input Format:

The first line contains numbers separated by a space.

Output Format:

Print maximum and minimum separated by a space.

Example:

Input:
1 2 3 4 5
Output:
5 1

Solution:

print("Enter numbers separated by spaces:")
inputStr = input()
strList = inputStr.split()
numList = [int(x) for x in strList]
min = numList[0]
max = numList[0]
for x in numList:
if x < min:
min = x
if x > max:
max = x
print("The minimum and maximum numbers are:")
print(min, max)

Output:

Enter numbers separated by spaces:
5 3 8 0 67 23 6
The minimum and maximum numbers are:
0 67


.

No comments: