Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Ari #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 18 additions & 16 deletions specs/matrix_convert_to_zero_spec.rb
Original file line number Diff line number Diff line change
@@ -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|
Expand All @@ -14,7 +15,7 @@ def initialize_matrix(rows, columns)
end
end

return matrix
matrix
end

# helper method for verifying updated matrix
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down