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

check matrix sum #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions lib/matrix_check_sum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,35 @@
# 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.
def matrix_check_sum(matrix)
# We are assuming all in coming matrices are square.
# Matrix.length is the number of rows in the matrix.
# Set index = 0, sum at row 0 and column 0
# row = matrix[0][0] + matrix [0][1] + matrix[0][2] + matrix[0][3]
# column = matrix[0][0] + matrix[1][0] + matrix[2][0] + matrix[3][0]
row_col_index = 0
row_sum = 0
column_sum = 0
while row_col_index < matrix.length do

# Add row
# Add column
index = 0
while index < matrix.length do
row_sum += matrix[row_col_index][index]
column_sum += matrix[index][row_col_index]
index += 1
end
# If the sum row == sum column, iterate row_col_index
if row_sum == column_sum
row_col_index += 1
# else sum row == sum column return false
else
return false
# end
end
# return true (iterated through entire matrix with corresponding rows and columns equal to eachother)

end
return true
raise NotImplementedError

Choose a reason for hiding this comment

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

Line 36 is dead code and should be safely deleted.

end