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

Daniela - matrix check sum #1

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: 32 additions & 1 deletion lib/matrix_check_sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@
# whether the sum of each row matches the sum of corresponding column i.e. sum
# of numbers in row i is the same as the sum of numbers in column i for i = 0 to row.length-1
# If this is the case, return true. Otherwise, return false.

#time complexity O(n^2), space complexity O(1)

def matrix_check_sum(matrix)
raise NotImplementedError
rows = matrix.size
columns = matrix[0].size

i = 0
until i == rows

rowsum = 0
col = 0
row = i
until col == columns # sum row
rowsum += matrix[row][col]
col += 1
end

colsum = 0
row = 0
col = i
until row == rows # sum column
colsum += matrix[row][col]
row += 1
end

return false if rowsum != colsum

i += 1

end

return true
end
1 change: 1 addition & 0 deletions specs/matrix_check_sum_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'minitest/autorun'
require 'minitest/reporters'
require 'minitest/skip_dsl'
require_relative '../lib/matrix_check_sum'

describe "matrix check sum" do
Expand Down