From 7e5d1e8a40905da90d30a20f49cf3a2a091054cb Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 29 Oct 2018 21:18:28 -0700 Subject: [PATCH 1/4] first attempts tests not passing --- lib/matrix_convert_to_zero.rb | 18 ++++++- specs/matrix_convert_to_zero_spec.rb | 72 ++++++++++++++-------------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index 4e7684b..cea008d 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -3,5 +3,21 @@ # If any number is found to be 0, the method updates all the numbers in the # corresponding row as well as the corresponding column to be 0. def matrix_convert_to_0(matrix) - raise NotImplementedError + puts "beginning #{matrix}" + (matrix.length - 1).times do |i| #number of rows + (matrix[i].length - 1).times do |j| #number of columns + if matrix[i][j] == 0 #if something in this row/column is 0 + row = i + column = j #select the column + (matrix[i].length - 1).times do |j| #change the whole row to 0 + matrix[row][j] = 0 + end + (matrix[i].length - 1).times do + matrix[i][column] = 0 + end + end + end + end + puts "end #{matrix}" + return matrix end diff --git a/specs/matrix_convert_to_zero_spec.rb b/specs/matrix_convert_to_zero_spec.rb index c9d88db..43eace6 100644 --- a/specs/matrix_convert_to_zero_spec.rb +++ b/specs/matrix_convert_to_zero_spec.rb @@ -53,41 +53,41 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - it "rows 0, 1, 2, 3, 4, column 1 are 0" do - # setup - rows = 5 - columns = 3 - matrix = initialize_matrix(rows, columns) - matrix[0][1] = 0 # row 0, column 1 - matrix[1][1] = 0 # row 1, column 1 - matrix[2][1] = 0 # row 2, column 1 - matrix[3][1] = 0 # row 3, column 1 - matrix[4][1] = 0 # row 4, column 1 - rows_array = [0, 1, 2, 3, 4] - columns_array = [1] - - # method call - matrix_convert_to_0(matrix) - - # validation - verify_matrix(matrix, rows_array, columns_array) - end - end - - describe "edge case" do - it "no 0s" do - # setup - rows = 4 - columns = 4 - matrix = initialize_matrix(rows, columns) - rows_array = [] - columns_array = [] - - # method call - matrix_convert_to_0(matrix) - - # validation - verify_matrix(matrix, rows_array, columns_array) - end + # it "rows 0, 1, 2, 3, 4, column 1 are 0" do + # # setup + # rows = 5 + # columns = 3 + # matrix = initialize_matrix(rows, columns) + # matrix[0][1] = 0 # row 0, column 1 + # matrix[1][1] = 0 # row 1, column 1 + # matrix[2][1] = 0 # row 2, column 1 + # matrix[3][1] = 0 # row 3, column 1 + # matrix[4][1] = 0 # row 4, column 1 + # rows_array = [0, 1, 2, 3, 4] + # columns_array = [1] + # + # # method call + # matrix_convert_to_0(matrix) + # + # # validation + # verify_matrix(matrix, rows_array, columns_array) + # end + # end + # + # describe "edge case" do + # it "no 0s" do + # # setup + # rows = 4 + # columns = 4 + # matrix = initialize_matrix(rows, columns) + # rows_array = [] + # columns_array = [] + # + # # method call + # matrix_convert_to_0(matrix) + # + # # validation + # verify_matrix(matrix, rows_array, columns_array) + # end end end From d09acebfcae38bdebcef44d150fe65065878c278 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 31 Oct 2018 22:06:30 -0700 Subject: [PATCH 2/4] second attmpt --- lib/matrix_convert_to_zero.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index cea008d..b3e8495 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -4,15 +4,15 @@ # corresponding row as well as the corresponding column to be 0. def matrix_convert_to_0(matrix) puts "beginning #{matrix}" - (matrix.length - 1).times do |i| #number of rows - (matrix[i].length - 1).times do |j| #number of columns + (matrix.length - 1).times do |i| #number of rows times do runs 3 times + (matrix[i].length - 1).times do |j| #number of columns this runs 4 times if matrix[i][j] == 0 #if something in this row/column is 0 row = i column = j #select the column - (matrix[i].length - 1).times do |j| #change the whole row to 0 + (matrix[row].length - 1).times do |j| #change the whole row to 0 matrix[row][j] = 0 end - (matrix[i].length - 1).times do + (matrix.length - 1).times do |i| matrix[i][column] = 0 end end @@ -21,3 +21,6 @@ def matrix_convert_to_0(matrix) puts "end #{matrix}" return matrix end +# loop1 +# i = 0 (goes up to 2) +# j = 0 (goes up to 4) From 79d7fa72f45bd61830172c01ef378c5ac70025d7 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 31 Oct 2018 22:24:01 -0700 Subject: [PATCH 3/4] matrix tests passing yay --- lib/matrix_convert_to_zero.rb | 11 ++--- specs/matrix_convert_to_zero_spec.rb | 72 ++++++++++++++-------------- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index b3e8495..b180203 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -4,15 +4,15 @@ # corresponding row as well as the corresponding column to be 0. def matrix_convert_to_0(matrix) puts "beginning #{matrix}" - (matrix.length - 1).times do |i| #number of rows times do runs 3 times - (matrix[i].length - 1).times do |j| #number of columns this runs 4 times + (matrix.length).times do |i| #number of rows times do runs 3 times + (matrix[i].length).times do |j| #number of columns this runs 4 times if matrix[i][j] == 0 #if something in this row/column is 0 row = i column = j #select the column - (matrix[row].length - 1).times do |j| #change the whole row to 0 + (matrix[row].length).times do |j| #change the whole row to 0 matrix[row][j] = 0 end - (matrix.length - 1).times do |i| + (matrix.length).times do |i| matrix[i][column] = 0 end end @@ -21,6 +21,3 @@ def matrix_convert_to_0(matrix) puts "end #{matrix}" return matrix end -# loop1 -# i = 0 (goes up to 2) -# j = 0 (goes up to 4) diff --git a/specs/matrix_convert_to_zero_spec.rb b/specs/matrix_convert_to_zero_spec.rb index 43eace6..c9d88db 100644 --- a/specs/matrix_convert_to_zero_spec.rb +++ b/specs/matrix_convert_to_zero_spec.rb @@ -53,41 +53,41 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - # it "rows 0, 1, 2, 3, 4, column 1 are 0" do - # # setup - # rows = 5 - # columns = 3 - # matrix = initialize_matrix(rows, columns) - # matrix[0][1] = 0 # row 0, column 1 - # matrix[1][1] = 0 # row 1, column 1 - # matrix[2][1] = 0 # row 2, column 1 - # matrix[3][1] = 0 # row 3, column 1 - # matrix[4][1] = 0 # row 4, column 1 - # rows_array = [0, 1, 2, 3, 4] - # columns_array = [1] - # - # # method call - # matrix_convert_to_0(matrix) - # - # # validation - # verify_matrix(matrix, rows_array, columns_array) - # end - # end - # - # describe "edge case" do - # it "no 0s" do - # # setup - # rows = 4 - # columns = 4 - # matrix = initialize_matrix(rows, columns) - # rows_array = [] - # columns_array = [] - # - # # method call - # matrix_convert_to_0(matrix) - # - # # validation - # verify_matrix(matrix, rows_array, columns_array) - # end + it "rows 0, 1, 2, 3, 4, column 1 are 0" do + # setup + rows = 5 + columns = 3 + matrix = initialize_matrix(rows, columns) + matrix[0][1] = 0 # row 0, column 1 + matrix[1][1] = 0 # row 1, column 1 + matrix[2][1] = 0 # row 2, column 1 + matrix[3][1] = 0 # row 3, column 1 + matrix[4][1] = 0 # row 4, column 1 + rows_array = [0, 1, 2, 3, 4] + columns_array = [1] + + # method call + matrix_convert_to_0(matrix) + + # validation + verify_matrix(matrix, rows_array, columns_array) + end + end + + describe "edge case" do + it "no 0s" do + # setup + rows = 4 + columns = 4 + matrix = initialize_matrix(rows, columns) + rows_array = [] + columns_array = [] + + # method call + matrix_convert_to_0(matrix) + + # validation + verify_matrix(matrix, rows_array, columns_array) + end end end From 9442b1eae479f0bb59930dc86f3aec100147ec68 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 31 Oct 2018 22:53:28 -0700 Subject: [PATCH 4/4] added space and time complexity which might need revision --- lib/matrix_convert_to_zero.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index b180203..2f875ba 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -3,7 +3,6 @@ # If any number is found to be 0, the method updates all the numbers in the # corresponding row as well as the corresponding column to be 0. def matrix_convert_to_0(matrix) - puts "beginning #{matrix}" (matrix.length).times do |i| #number of rows times do runs 3 times (matrix[i].length).times do |j| #number of columns this runs 4 times if matrix[i][j] == 0 #if something in this row/column is 0 @@ -18,6 +17,21 @@ def matrix_convert_to_0(matrix) end end end - puts "end #{matrix}" return matrix end + +# Space Complexity: +# Regardless of the side of the matrix, we need to create two variables +#row and column to hold the position of the element that is 0. +# The row and column variables will only contain 1 integer each, +#which stays the same, thefore it is Constant O(1) Space Complexity. +# +# Time Complexity: +# There are 4 loops in this solution. The first loop will perform n times +# (where n is the number of rows that the matrix contains). The second loop +#will perform m times (where m is the number of columns the matrix contains). +# If 0 is found in a row/column combination, in the worst case the third and fourth +# loops will run row.length * column.length times, or n x n, or n**2. +# Overall, the time complexity is O(n*m) + n **2 +# +#