파이썬에서는 최대, 최소 요소 및 위치를 찾는 것이 매우 쉽습니다. Python은 다른 내장 기능을 제공합니다. min()은 배열의 최소값을 찾는 데 사용되며 max()는 배열의 최대값을 찾는 데 사용됩니다. index()는 요소의 인덱스를 찾는 데 사용됩니다.
알고리즘
maxminposition(A, n)
/* A is a user input list and n is the size of the list.*/
Step 1: use inbuilt function for finding the position of minimum element.
A.index(min(A))
Step 2: use inbuilt function for finding the position of a maximum element.
A.index(max(A))
예시 코드
# Function to find minimum and maximum position in list
def maxminposition(A, n):
# inbuilt function to find the position of minimum
minposition = A.index(min(A))
# inbuilt function to find the position of maximum
maxposition = A.index(max(A))
print ("The maximum is at position::", maxposition + 1)
print ("The minimum is at position::", minposition + 1)
# Driver code
A=list()
n=int(input("Enter the size of the List ::"))
print("Enter the Element ::")
for i in range(int(n)):
k=int(input(""))
A.append(k)
maxminposition(A,n)
출력
Enter the size of the List ::4 Enter the Element:: 12 34 1 66 The maximum is at position:: 4 The minimum is at position:: 3