From 6c21ab33fbba856bd02abe59cc355e814ddc1405 Mon Sep 17 00:00:00 2001 From: Muhammadrasul <64916997+Muhammadrasul446@users.noreply.github.com> Date: Mon, 8 Jun 2020 20:58:53 +0500 Subject: [PATCH 1/9] Add files via upload --- matrix/count_islands.py | 58 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 matrix/count_islands.py diff --git a/matrix/count_islands.py b/matrix/count_islands.py new file mode 100644 index 000000000000..a7c867ca877a --- /dev/null +++ b/matrix/count_islands.py @@ -0,0 +1,58 @@ +# An island in matrix is a group of linked areas, all having the same value. +# This code couts number of islands in a given matrix, with including diagonal connections. + +class matrix: # Public class to implement a graph + + def __init__(self, row, col, g): # Class constructor + self.ROW = row + self.COL = col + self.graph = g + + def is_safe(self, i, j, visited): + return (i >= 0 and i < self.ROW and j >= 0 and j < self.COL and not visited[i][j] and self.graph[i][j]) + + def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element + + rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]; # Coordinate order + colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]; + + visited[i][j] = True # Make those cells visited + + for k in range(8): + if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): + self.diffs(i + rowNbr[k], j + colNbr[k], visited) + + + def count_islands(self): # And finally, count all islandas. + visited = [[False for j in range(self.COL)]for i in range(self.ROW)] + count = 0 + for i in range(self.ROW): + for j in range(self.COL): + if visited[i][j] == False and self.graph[i][j] == 1: + self.diffs(i, j, visited) + count += 1 + return count + +# A test matrix +test_matrix = [ + [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], + [0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0], + [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], + [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1], + [0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0], + [0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0], + [0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], +] + + +row = len(graph) +col = len(graph[0]) + +matrix = matrix(row, col, test_matrix) + +print(matrix.count_islands()) From 3b9c007d90ce685f969bd53405604b4bd8d4c14c Mon Sep 17 00:00:00 2001 From: Muhammadrasul <64916997+Muhammadrasul446@users.noreply.github.com> Date: Mon, 8 Jun 2020 21:00:37 +0500 Subject: [PATCH 2/9] Update and rename count_islands.py to count_islands_in_matrix.py --- ..._islands.py => count_islands_in_matrix.py} | 23 ------------------- 1 file changed, 23 deletions(-) rename matrix/{count_islands.py => count_islands_in_matrix.py} (65%) diff --git a/matrix/count_islands.py b/matrix/count_islands_in_matrix.py similarity index 65% rename from matrix/count_islands.py rename to matrix/count_islands_in_matrix.py index a7c867ca877a..f4cb74194bc3 100644 --- a/matrix/count_islands.py +++ b/matrix/count_islands_in_matrix.py @@ -33,26 +33,3 @@ def count_islands(self): # And finally, count all islandas. count += 1 return count -# A test matrix -test_matrix = [ - [1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0], - [0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0], - [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], - [0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1], - [0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0], - [0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0], - [0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], - [0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0], - [0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], -] - - -row = len(graph) -col = len(graph[0]) - -matrix = matrix(row, col, test_matrix) - -print(matrix.count_islands()) From 02b3f97d27a36ce8a17f3eaf6ad991a22bdf4293 Mon Sep 17 00:00:00 2001 From: Muhammadrasul <64916997+Muhammadrasul446@users.noreply.github.com> Date: Tue, 9 Jun 2020 16:07:37 +0500 Subject: [PATCH 3/9] Update matrix/count_islands_in_matrix.py Co-authored-by: Christian Clauss --- matrix/count_islands_in_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index f4cb74194bc3..8817f92d173d 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -9,7 +9,7 @@ def __init__(self, row, col, g): # Class constructor self.graph = g def is_safe(self, i, j, visited): - return (i >= 0 and i < self.ROW and j >= 0 and j < self.COL and not visited[i][j] and self.graph[i][j]) + return 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element From 2a143af128162e533ef9a2a672a9882cc79a39c2 Mon Sep 17 00:00:00 2001 From: Muhammadrasul <64916997+Muhammadrasul446@users.noreply.github.com> Date: Tue, 9 Jun 2020 16:07:57 +0500 Subject: [PATCH 4/9] Update matrix/count_islands_in_matrix.py Co-authored-by: Christian Clauss --- matrix/count_islands_in_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 8817f92d173d..b17cc2703aee 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -28,7 +28,7 @@ def count_islands(self): # And finally, count all islandas. count = 0 for i in range(self.ROW): for j in range(self.COL): - if visited[i][j] == False and self.graph[i][j] == 1: + if visited[i][j] is False and self.graph[i][j] == 1: self.diffs(i, j, visited) count += 1 return count From 2db701654f30504e74c8d1839a870d770b0a9383 Mon Sep 17 00:00:00 2001 From: Muhammadrasul <64916997+Muhammadrasul446@users.noreply.github.com> Date: Tue, 9 Jun 2020 16:08:08 +0500 Subject: [PATCH 5/9] Update matrix/count_islands_in_matrix.py Co-authored-by: Christian Clauss --- matrix/count_islands_in_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index b17cc2703aee..31d2e447a443 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -3,7 +3,7 @@ class matrix: # Public class to implement a graph - def __init__(self, row, col, g): # Class constructor + def __init__(self, row: int, col: int, graph: list): # Class constructor self.ROW = row self.COL = col self.graph = g From a4e669621885f540a441990c0c59b24be08eb568 Mon Sep 17 00:00:00 2001 From: Muhammadrasul Date: Tue, 9 Jun 2020 17:29:50 +0500 Subject: [PATCH 6/9] Reformat count islands.py --- matrix/count_islands_in_matrix.py | 54 +++++++++++++++---------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 31d2e447a443..d3aa94e12abf 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -1,35 +1,33 @@ -# An island in matrix is a group of linked areas, all having the same value. +# An island in matrix is a group of linked areas, all having the same value. # This code couts number of islands in a given matrix, with including diagonal connections. -class matrix: # Public class to implement a graph - - def __init__(self, row: int, col: int, graph: list): # Class constructor - self.ROW = row - self.COL = col +class matrix: # Public class to implement a graph + + def __init__(self, row: int, col: int, graph: list): # Class constructor + self.ROW = row + self.COL = col self.graph = g - def is_safe(self, i, j, visited): + def is_safe(self, i, j, visited): return 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] - - def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element - - rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1]; # Coordinate order - colNbr = [-1, 0, 1, -1, 1, -1, 0, 1]; - - visited[i][j] = True # Make those cells visited - - for k in range(8): - if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): - self.diffs(i + rowNbr[k], j + colNbr[k], visited) - - - def count_islands(self): # And finally, count all islandas. - visited = [[False for j in range(self.COL)]for i in range(self.ROW)] + + def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element + + rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order + colNbr = [-1, 0, 1, -1, 1, -1, 0, 1] + + visited[i][j] = True # Make those cells visited + + for k in range(8): + if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): + self.diffs(i + rowNbr[k], j + colNbr[k], visited) + + def count_islands(self): # And finally, count all islands. + visited = [[False for j in range(self.COL)]for i in range(self.ROW)] count = 0 - for i in range(self.ROW): - for j in range(self.COL): - if visited[i][j] is False and self.graph[i][j] == 1: - self.diffs(i, j, visited) + for i in range(self.ROW): + for j in range(self.COL): + if visited[i][j] is False and self.graph[i][j] == 1: + self.diffs(i, j, visited) count += 1 - return count - + return count From 1d512db13cc4175a8b1cfd7fb6b9191be20a1687 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 17:44:00 +0200 Subject: [PATCH 7/9] Indent Python code with 4 spaces, not tabs --- matrix/count_islands_in_matrix.py | 66 +++++++++++++++++-------------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index d3aa94e12abf..8315c79599a2 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -1,33 +1,39 @@ # An island in matrix is a group of linked areas, all having the same value. -# This code couts number of islands in a given matrix, with including diagonal connections. +# This code couts number of islands in a given matrix, with including diagonal +# connections. -class matrix: # Public class to implement a graph - - def __init__(self, row: int, col: int, graph: list): # Class constructor - self.ROW = row - self.COL = col - self.graph = g - - def is_safe(self, i, j, visited): - return 0 <= i < self.ROW and 0 <= j < self.COL and not visited[i][j] and self.graph[i][j] - - def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element - rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order - colNbr = [-1, 0, 1, -1, 1, -1, 0, 1] - - visited[i][j] = True # Make those cells visited - - for k in range(8): - if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): - self.diffs(i + rowNbr[k], j + colNbr[k], visited) - - def count_islands(self): # And finally, count all islands. - visited = [[False for j in range(self.COL)]for i in range(self.ROW)] - count = 0 - for i in range(self.ROW): - for j in range(self.COL): - if visited[i][j] is False and self.graph[i][j] == 1: - self.diffs(i, j, visited) - count += 1 - return count +class matrix: # Public class to implement a graph + def __init__(self, row: int, col: int, graph: list): # Class constructor + self.ROW = row + self.COL = col + self.graph = graph + + def is_safe(self, i, j, visited): + return ( + 0 <= i < self.ROW + and 0 <= j < self.COL + and not visited[i][j] + and self.graph[i][j] + ) + + def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element + + rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order + colNbr = [-1, 0, 1, -1, 1, -1, 0, 1] + + visited[i][j] = True # Make those cells visited + + for k in range(8): + if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): + self.diffs(i + rowNbr[k], j + colNbr[k], visited) + + def count_islands(self): # And finally, count all islands. + visited = [[False for j in range(self.COL)] for i in range(self.ROW)] + count = 0 + for i in range(self.ROW): + for j in range(self.COL): + if visited[i][j] is False and self.graph[i][j] == 1: + self.diffs(i, j, visited) + count += 1 + return count From cf1c8ec05b193b17d91b478d1ec02e7d91a3fd27 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 9 Jun 2020 17:48:54 +0200 Subject: [PATCH 8/9] Update count_islands_in_matrix.py --- matrix/count_islands_in_matrix.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 8315c79599a2..69a3858cf141 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -1,5 +1,5 @@ # An island in matrix is a group of linked areas, all having the same value. -# This code couts number of islands in a given matrix, with including diagonal +# This code counts number of islands in a given matrix, with including diagonal # connections. @@ -18,12 +18,9 @@ def is_safe(self, i, j, visited): ) def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth element - rowNbr = [-1, -1, -1, 0, 0, 1, 1, 1] # Coordinate order colNbr = [-1, 0, 1, -1, 1, -1, 0, 1] - visited[i][j] = True # Make those cells visited - for k in range(8): if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): self.diffs(i + rowNbr[k], j + colNbr[k], visited) From 0d5c5d55aa952fa42f2f216606fafd186b737194 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 16 Jun 2020 14:15:54 +0200 Subject: [PATCH 9/9] Add type hints for return values --- matrix/count_islands_in_matrix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix/count_islands_in_matrix.py b/matrix/count_islands_in_matrix.py index 69a3858cf141..ad9c67fb8c1b 100644 --- a/matrix/count_islands_in_matrix.py +++ b/matrix/count_islands_in_matrix.py @@ -4,12 +4,12 @@ class matrix: # Public class to implement a graph - def __init__(self, row: int, col: int, graph: list): # Class constructor + def __init__(self, row: int, col: int, graph: list): self.ROW = row self.COL = col self.graph = graph - def is_safe(self, i, j, visited): + def is_safe(self, i, j, visited) -> bool: return ( 0 <= i < self.ROW and 0 <= j < self.COL @@ -25,7 +25,7 @@ def diffs(self, i, j, visited): # Checking all 8 elements surrounding nth eleme if self.is_safe(i + rowNbr[k], j + colNbr[k], visited): self.diffs(i + rowNbr[k], j + colNbr[k], visited) - def count_islands(self): # And finally, count all islands. + def count_islands(self) -> int: # And finally, count all islands. visited = [[False for j in range(self.COL)] for i in range(self.ROW)] count = 0 for i in range(self.ROW):