Matrix python function












0















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









share|improve this question

























  • Also diagonally? Starting from anywhere?

    – iGian
    Nov 12 '18 at 16:22













  • @iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

    – John.Doe
    Nov 12 '18 at 16:36
















0















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









share|improve this question

























  • Also diagonally? Starting from anywhere?

    – iGian
    Nov 12 '18 at 16:22













  • @iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

    – John.Doe
    Nov 12 '18 at 16:36














0












0








0








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









share|improve this question
















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






python matrix






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 16:32









Adam Mitchell

7581627




7581627










asked Nov 12 '18 at 15:58









John.DoeJohn.Doe

144




144













  • Also diagonally? Starting from anywhere?

    – iGian
    Nov 12 '18 at 16:22













  • @iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

    – John.Doe
    Nov 12 '18 at 16:36



















  • Also diagonally? Starting from anywhere?

    – iGian
    Nov 12 '18 at 16:22













  • @iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

    – John.Doe
    Nov 12 '18 at 16:36

















Also diagonally? Starting from anywhere?

– iGian
Nov 12 '18 at 16:22







Also diagonally? Starting from anywhere?

– iGian
Nov 12 '18 at 16:22















@iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

– John.Doe
Nov 12 '18 at 16:36





@iGian yeah the position can be anywhere as long as it's in the matrix. Whatever the position will be, it will check all its adjacent positions (row -1, column)(row +1, column) (row, column - 1) etc. and the smallest value from those adjacent positions will be the new position and we do the comparison again until it can no longer find any or we reached the smallest like the example stated above.

– John.Doe
Nov 12 '18 at 16:36












1 Answer
1






active

oldest

votes


















0














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]]





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265794%2fmatrix-python-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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]]





    share|improve this answer




























      0














      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]]





      share|improve this answer


























        0












        0








        0







        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]]





        share|improve this answer













        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]]






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 '18 at 21:20









        iGianiGian

        3,5952622




        3,5952622






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265794%2fmatrix-python-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation