diff --git a/lib/matrix_convert_to_zero.rb b/lib/matrix_convert_to_zero.rb index 4fb139c..f349e54 100644 --- a/lib/matrix_convert_to_zero.rb +++ b/lib/matrix_convert_to_zero.rb @@ -3,8 +3,27 @@ # 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. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n*m) + O (n^2*m^2), where n is the number of rows and m is the number of columns +# Space complexity: O(n +m) def matrix_convert_to_zero(matrix) - raise NotImplementedError + return nil if matrix.empty? + + rows = matrix.size + columns = matrix[0].size + + rows.times do |row| + columns.times do |column| + matrix[row][column] = 'x' if matrix[row][column] == 0 + end + end + + rows.times do |row| + columns.times do |column| + next unless matrix[row][column] == 'x' + + columns.times { |i| matrix[row][i] = 0 unless matrix[row][i] == 'x' } + rows.times { |i| matrix[i][column] = 0 unless matrix[i][column] == 'x' } + matrix[row][column] = 0 + end + end end diff --git a/specs/matrix_convert_to_zero_spec.rb b/specs/matrix_convert_to_zero_spec.rb index a21b0a6..d31ed15 100644 --- a/specs/matrix_convert_to_zero_spec.rb +++ b/specs/matrix_convert_to_zero_spec.rb @@ -1,11 +1,12 @@ require 'minitest/autorun' require 'minitest/reporters' require_relative '../lib/matrix_convert_to_zero' +require 'pry' # helper method for creating and initializing a matrix with all 1s def initialize_matrix(rows, columns) # create the matrix using the rows and columns - matrix = Array.new(rows){Array.new(columns)} + matrix = Array.new(rows) { Array.new(columns) } # initialize the matrix rows.times do |row| @@ -14,7 +15,7 @@ def initialize_matrix(rows, columns) end end - return matrix + matrix end # helper method for verifying updated matrix @@ -25,7 +26,7 @@ def verify_matrix(matrix, rows_array, columns_array) rows.times do |row| columns.times do |column| - if (rows_array.include?(row) || columns_array.include?(column)) + if rows_array.include?(row) || columns_array.include?(column) matrix[row][column].must_equal 0 else matrix[row][column].must_equal 1 @@ -34,13 +35,14 @@ def verify_matrix(matrix, rows_array, columns_array) end end -describe "matrix convert to zero" do - describe "basic tests" do - it "rows 1 & 2, columns 3 & 4 are 0" do +describe 'matrix convert to zero' do + describe 'basic tests' do + it 'rows 1 & 2, columns 3 & 4 are 0' do # setup rows = 3 columns = 5 matrix = initialize_matrix(rows, columns) + matrix[1][3] = 0 # row 1, column 3 matrix[2][4] = 0 # row 2, column 4 rows_array = [1, 2] @@ -53,7 +55,7 @@ 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 + it 'rows 0, 1, 2, 3, 4, column 1 are 0' do # setup rows = 5 columns = 3 @@ -73,8 +75,8 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - it "only first cell is zero" do - #setup + it 'only first cell is zero' do + # setup rows = 3 columns = 6 matrix = initialize_matrix(rows, columns) @@ -89,8 +91,8 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - it "only middle cell is zero" do - #setup + it 'only middle cell is zero' do + # setup rows = 5 columns = 7 matrix = initialize_matrix(rows, columns) @@ -105,8 +107,8 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - it "only the last cell is zero" do - #setup + it 'only the last cell is zero' do + # setup rows = 8 columns = 7 matrix = initialize_matrix(rows, columns) @@ -121,7 +123,7 @@ def verify_matrix(matrix, rows_array, columns_array) verify_matrix(matrix, rows_array, columns_array) end - it "not all rows, not all columns" do + it 'not all rows, not all columns' do # setup rows = 4 columns = 5 @@ -140,8 +142,8 @@ def verify_matrix(matrix, rows_array, columns_array) end end - describe "edge case" do - it "no 0s" do + describe 'edge case' do + it 'no 0s' do # setup rows = 4 columns = 4