Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find maximum length Snake sequence in Python
Suppose we have a grid of numbers; we have to find a snake sequence and return it. If there are multiple snake sequence, then return only one. As we know a snake sequence is made using adjacent numbers in the grid so for each number, the number on the right-hand side or the number below it is either +1 or -1 its value. So, if current value is in grid cell (a, b), we can either move right (a, b+1) if that number is ± 1 or move below (a+1, b) if that number is ± 1.
So, if the input is like
| 10 | 7 | 6 | 3 |
| 9 | 8 | 7 | 6 |
| 8 | 4 | 2 | 7 |
| 2 | 2 | 2 | 8 |
then the output will be 6, sequence − 10 (0, 0) to 9 (1, 0) to 8 (1, 1) to 7 (1, 2) to 6 (1, 3) to 7 (2, 3) to 8 (3, 3)
To solve this, we will follow these steps −
Define a function get_path() . This will take grid, mat, i, j
path := a new list
pt := a point [i, j]
insert pt at the end of path
-
while grid[i, j] is not 0, do
-
if i > 0 and grid[i, j]-1 is same as grid[i-1, j], then
pt := [i-1, j]
insert pt at the end of path
i := i - 1
-
otherwise when j > 0 and grid[i, j]-1 is same as grid[i, j-1], then
pt := [i, j-1]
insert pt at the end of path
j := j - 1
-
return path
From the main method, do the following −
lookup := make a grid of size M x N and fill with 0
length_max := 0, max_row := 0, max_col := 0
-
for i in range 0 to M, do
-
for j in range 0 to N, do
-
if i or j is non-zero, then
-
if (i > 0 an and |grid[i-1, j] - grid[i, j]| is 1, then
lookup[i,j] = maximum of lookup[i,j],lookup[i-1,j] + 1)
-
if length_max < lookup[i,j], then
length_max := lookup[i, j]
max_row := i
max_col := j
-
if (j > 0 and |grid[i, j-1] - grid[i, j]| is 1, then
if length_max < lookup[i][j] is non-zero, then
-
lookup[i,j] = maximum of lookup[i,j],lookup[i,j-1] + 1)
length_max := lookup[i, j]
max_row := i
max_col := j
-
-
-
display length_max
path := get_path(lookup, grid, max_row, max_col)
print all elements in path in reverse order
Example
Let us see the following implementation to get better understanding &mius;
M = 4
N = 4
def get_path(grid, mat, i, j):
path = list()
pt = [i, j]
path.append(pt)
while (grid[i][j] != 0):
if (i > 0 and grid[i][j]-1 == grid[i-1][j]):
pt = [i-1, j]
path.append(pt)
i -= 1
elif (j > 0 and grid[i][j]-1 == grid[i][j-1]):
pt = [i, j-1]
path.append(pt)
j -= 1
return path
def get_sequence(grid):
lookup = [[0 for i in range(N)] for j in range(M)]
length_max = 0
max_row = 0
max_col = 0
for i in range(M):
for j in range(N):
if (i or j):
if (i > 0 and
abs(grid[i-1][j] - grid[i][j]) == 1):
lookup[i][j] = max(lookup[i][j],lookup[i-1][j] + 1)
if (length_max < lookup[i][j]):
length_max = lookup[i][j]
max_row = i
max_col = j
if (j > 0 and
abs(grid[i][j-1] - grid[i][j]) == 1):
lookup[i][j] = max(lookup[i][j],lookup[i][j-1] + 1)
if (length_max < lookup[i][j]):
length_max = lookup[i][j]
max_row = i
max_col = j
print("Maximum length:", length_max)
path = get_path(lookup, grid, max_row, max_col)
print("Sequence is:")
for ele in reversed(path):
print(grid[ele[0]][ele[1]], " [", ele[0], ", ", ele[1], "]", sep = "")
grid = [
[10, 7, 6, 3],
[9, 8, 7, 6],
[8, 4, 2, 7],
[2, 2, 2, 8]]
get_sequence(grid)
Input
[[10, 7, 6, 3], [9, 8, 7, 6], [8, 4, 2, 7], [2, 2, 2, 8]]
Output
Maximum length: 6 Sequence is: 10 [0, 0] 9 [1, 0] 8 [1, 1] 7 [1, 2] 6 [1, 3] 7 [2, 3] 8 [3, 3]