Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement all the given functions that are used to solve the below problems. Follow the path You are given a matrix of size m x

Implement all the given functions that are used to solve the below problems.
Follow the path
You are given a matrix of size m x n consisting of ones (1) and zeros (0). There is a single continuous path formed with ones that starts from the rightmost cell in the last row (m-th row) with one and ends at leftmost cell in the first row with one in it. The path does not branch, and there is only one such path. Your task is to traverse along the path and print the coordinates of the path from start to end as tuples over multiple lines. The path can move vertically and horizontally.
Input
matrix =[
[0,0,1,1],
[0,0,0,1],
[1,1,1,1],
[1,0,0,0],
[1,1,0,0]
]
Output
(4,1)
(4,0)
(3,0)
(2,0)
(2,1)
(2,2)
(2,3)
(1,3)
(0,3)
(0,2)
def index_of_first_occurance(row:list,elem):
'''
Given a list find the index of first occurance of 1 in it
'''
...
def index_of_last_occurance(row:list,elem):
'''
Given a list find the index of last occurance of 1 in it.
Hint: use index_of_first_one with reversal.
'''
...
def is_valid_coordinate(x:int,y:int, M):
'''
Checks if the x,y is a valid corrdinate(indices) in the matrix M(list of list). Assume coordinates are non-negative
'''
...
def valid_adjacent_coordinates(x:int,y:int, M):
'''
Create a set of valid adjacent coordinates(indices) given x,y and a matrix M
'''
return {
(x1,y1)
for x1,y1 in ... # all the possible adjacent coordinates
if is_valid_coordinate(x1,y1, M)
}
def next_coordinate_with_value(curr_coords, value, M, prev_coords=None):
'''
Find the coordinate(indices) of the next coordinate that has the `value` in it. For the starting coordinate the prev_coords would be None
'''
...
def get_path_coordinates(M):
'''
Given the matrix m, find the path formed by 1 from the last row to the first row.
'''
x_start, x_end = len(M)-1,0
y_start, y_end = index_of_last_occurance(M[-1],1), index_of_first_occurance(M[0],1)
...
def print_path(M):
path = get_path_coordinates(M)
...
def alternate_path(M):
path = get_path_coordinates(M)
...
def count_path(M):
path = get_path_coordinates(M)
...
def mirror_horizontally(M):
path = get_path_coordinates(M)
...
def mirror_vertically(M):
path = get_path_coordinates(M)
...

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MySQL/PHP Database Applications

Authors: Jay Greenspan, Brad Bulger

1st Edition

978-0764535376

More Books

Students also viewed these Databases questions

Question

1. Identify and control your anxieties

Answered: 1 week ago