Question: FIVE MINS LEFT. Given an N*M matrix and starting from cell (1,1), her challenge is to hop in an anti-clockwise direction and skip alternate cells.
FIVE MINS LEFT. Given an N*M matrix and starting from cell (1,1), her challenge is to hop in an anti-clockwise direction and skip alternate cells. The goal is to find out the last cell she would hop onto. Write an algorithm to find the last cell she would hop onto. Write an algorithm to find the last cell Lucy would hop onto after moving anti-clockwise and skipping alternate cells. Input: The first line of input consists of two integers-matrix_row and matrix_col, representing the number of rows (N) and the number of columns (M) in the matrix, respectively. The next M lines consist of N space-separated integers representing the elements in each cell of the matrix. Output: print an integer representing the last cell lucy would hop onto after the following instructions. example: input: 3 3 29 8 37 15 41 3 1 10 14 Output: 41 your function should look like this: def funcHopSkipJump(matrix): # Write your code here What I have so far (passing 5/9 tests).: def funcHopSkipJump(matrix): row, col = 0, 0 direction = (-1, 1) while True: row += direction[0] col += direction[1] if row < 0 or row >= len(matrix) or col < 0 or col >= len(matrix[0]): if len(matrix) % 2 == 1 and len(matrix[0]) % 2 == 1: return matrix[len(matrix)//2][len(matrix[0])//2] else: return matrix[row - direction[0]][col - direction[1]] row += direction[0] col += direction[1] if row < 0 or row >= len(matrix) or col < 0 or col >= len(matrix[0]): if len(matrix) % 2 == 1 and len(matrix[0]) % 2 == 1: return matrix[len(matrix)//2][len(matrix[0])//2] else: row -= direction[0] col -= direction[1] direction = (direction[1], -direction[0])
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
