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 - Amy M #10

Open
wants to merge 1 commit 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
38 changes: 30 additions & 8 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
# Updates the input matrix based on the following rules:
# Assumption/ Given: All numbers in the matrix are 0s or 1s
# 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) where n is the number of rows and m is the number of columns.
# Space complexity: O(n + m), where n represents the number of rows that contain a zero, and m
# represents the number of columns that contain zero.
def matrix_convert_to_zero(matrix)
raise NotImplementedError
rows = matrix.length
cols = matrix[0].length
row_zero = []
col_zero = []

rows.times do |row|
cols.times do |col|
if matrix[row][col] == 0
row_zero << row
col_zero << col
end
end
end

row_zero.uniq.each do |row|
cols.times do |col|
matrix[row][col] = 0
end
end

col_zero.uniq.each do |col|
rows.times do |row|
matrix[row][col] = 0
end
end

return matrix
end
9 changes: 5 additions & 4 deletions specs/matrix_convert_to_zero_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/matrix_convert_to_zero'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/matrix_convert_to_zero"

# 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 Down Expand Up @@ -45,6 +45,7 @@ def verify_matrix(matrix, rows_array, columns_array)
matrix[2][4] = 0 # row 2, column 4
rows_array = [1, 2]
columns_array = [3, 4]
binding.pry

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have left a pry in :)


# method call
matrix_convert_to_zero(matrix)
Expand Down