简体   繁体   中英

Matrix python function

I have to write a function whereby given a matrix that is non empty and not negative, a starting position (a nonnegative list of length 2 that represents the position of the matrix) shorthand for the matrix[i][j] , it will return the position of the lowest adjacent value (keeps going until it finds the lowest only going through its adjacent cells/elements).

example: if matrix = [[8,90,91,73],[60,6,32,84],[50,4,45,94],[12,85,3,2]] and sarting position = [0,0] , it should return [3,3] . I want to try to implement this function with only basics of programming (if statements, loops etc.) simple helper functions like min or max can be used but nothing too advanced please.

This is what I have so far but it doesn't really work nicely as I have the index out or range errors:

def search_local_lowest_value(s: List[List[int]], position: List[int]) -> List[int]:
element1 = position[0]
element2 = position[1]
local_variable = s[element1][element2]

while local_variable <= s[row + 1][column] or local_variable <= s[row + 1][column + 1] \
    or local_variable <= s[row + 1][column - 1] or local_variable <= s[row][column + 1] \
    or local_variable <= s[row][column - 1] or local_variable <= s[row -1][column] \
    or local_variable <= s[row - 1][column + 1] or local_varaible <= s[row - 1][column - 1]:
        if local variable <= s[row + 1][column] or local_variable <= s[row + 1][column + 1] \
        or local_variable <= s[row + 1][column - 1] or local_variable <= s[row][column + 1] \
        or local_variable <= s[row][column - 1] or local_variable <= s[row -1][column] \
        or local_variable <= s[row - 1][column + 1] or local_variable <= s[row - 1][column - 1]:
            return min() // don't know what to fill for that
        else: 
            return False

I came out with this solution, I used methods to be more clear and dry:

def bordering(start, shape):
  PATTERN = [[-1,-1], [-1, 0], [-1, 1], [0, 0], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]
  border = [[start[0] + x, start[1] + y] for x, y in PATTERN]
  valid_border = [item for item in border if item[0] >= 0 and item[1] >= 0 and item[0] <= shape[0] - 1   and item[1] <= shape[1] - 1  ] # removes borders out of the matrix boundaries
  return valid_border

def smaller_from(start, matrix):
  shape = [len(matrix), len(matrix[0])]
  borders = bordering(start, shape)
  minimum = min([ [matrix[x][y], [x,y]] for x,y in borders ]) # maps the values and coordinates in valid borders and returns the minimum
  return minimum

def find_minimum(start, matrix):
  result = []
  while True:
    val_coords = smaller_from(start, matrix)
    result = val_coords
    if val_coords[0] >= matrix[start[0]][start[1]]:
      break
    else:
      start = val_coords[1]
  return result

matrix = [
          [8,90,91,73],
          [60,6,32,84],
          [50,4,45,94],
          [12,85,3,2]]
start = [0, 0]

find_minimum(start, matrix) #=> [2, [3, 3]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM