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

Sockets - Cyndi #6

Open
wants to merge 2 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
33 changes: 30 additions & 3 deletions lib/matrix_convert_to_zero.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,35 @@
# 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: the time complexity is O(n*m) where n is the number of rows in the matrix and m is the number of columns in the matrix because I have two nested loops O(2*n*m), which dropping the constant reduces to O(n*m)
# Space complexity: space complexity is O(n) where n is the larger number of rows or columns. This is because a hash is created where the rows are keys and columns are values
def matrix_convert_to_zero(matrix)
raise NotImplementedError
num = 0
pos = Hash.new
rows = matrix.size
columns = matrix[0].size

rows.times do |row|
columns.times do |col|
if matrix[row][col] == 0
if pos.include? row

Choose a reason for hiding this comment

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

I'm not sure the point of this if statement.

I suggest instead of this if-else statement just having two hashes, one for the rows and the other for columns.

num += 1
pos["row" + num.to_s] = col
else
pos[row] = col
end
end
end
end
rows.times do |row|
columns.times do |col|
if pos.include? row

Choose a reason for hiding this comment

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

I suggest reducing the two if statements to: if pos.include? row || pos.values.include? col

However remember the .include? is a loop, so this is another nested loop.

matrix[row][col] = 0
end
if pos.values.include? col
matrix[row][col] = 0
end
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 @@ -129,6 +129,7 @@ def verify_matrix(matrix, rows_array, columns_array)
matrix[0][3] = 0 # row 0, column 1
matrix[0][4] = 0 # row 1, column 1
matrix[2][1] = 0 # row 2, column 1
p matrix
rows_array = [0, 2]
columns_array = [1, 3, 4]

Expand Down