20190110 - finding saddle point of two-dimensional matrix by stupid method

Keywords: Python

1: find the saddle point of a multidimensional array, that is, the element is the largest in the row, the smallest in the column, or there may be no saddle point

a = [
    [1,2,3,4],
    [4,5,6,2],
    [7,0,5,2],
    [11,10,7,9]]

The solutions are as follows:

step1: first find out the maximum value of each row and its index, and output the maximum value, that is, all the rows and columns are stored in the result list as a list. The function is written as follows:

def find_saddle_max_point(s):
    result =[]
    #use result Store the maximum value of each line and its index
    for i in range(len(s)):
        #The number of iterations, i.e. how many rows are there in total, for example, there are 4 rows in total in the example, the value is equal to a Number of elements of
        max =s[i][0]
#Set the maximum value of each line as the first
        location =[]
#Build a new one. location List to store the value in the outer index Inner layer index
        index=0
#Of the column in which the maximum value is set index The value is 0.
        for j in range(len(s[i])):           
            #Traverse each value of each row
            if s[i][j]>max:
                max = s[i][j]
                index =j
#Find the maximum value and the column in which the maximum value is located, that is j Value
        location.append(i)
        location.append(index)
#Store horizontal list coordinates and vertical coordinate values
        result.append((max,location))
#The maximum value and location(Including horizontal and vertical coordinates)Save as result in
    return result
print(find_saddle_max_point(a))

step2: find out the minimum value of each column of the two-dimensional matrix, and write the function in the same way as step1

def find_saddle_min_point(s):
    result =[]
    for i in range(len(s[0])):
        #The number of times the column was traversed, such as a There are four columns, and the number of iterations of the columns is equal to a The length of the nested list for
        min = s[0][i]
#Set the minimum value to be the first in each column
        location=[]
        index=0
        for j in range(len(s)):
            #Traverse the value of each column
            if s[j][i]<min:
                min =s[j][i]
                index = j
        location.append(index)
        location.append(i)
#The minimum value of each column index Save as location List
        result.append((min,location))
#The minimum value of each column and the location Save as result in
    return result
print(find_saddle_min_point(a))

Step 3: traverse the list generated by step 1 and step 2. If there is a repetition of the maximum value and the minimum value, and the index is the same, that is, if there are duplicate elements in the two lists, it is the saddle point of the two-dimensional array

#step3: If the minimum value is the same as the maximum value, and index Same, saddle point
def find_saddle(a):
    list1 = find_saddle_max_point(a)
    list2 = find_saddle_min_point(a)
    for i in list1:
        if i in list2:
            return i
    else:
        return False
print(find_saddle(a))

Posted by SlimSlyk on Sun, 01 Dec 2019 01:45:43 -0800