Skip to content

Instantly share code, notes, and snippets.

@rlafranchi
Last active March 24, 2016 02:32
Show Gist options
  • Save rlafranchi/ee424d1d563d315e9176 to your computer and use it in GitHub Desktop.
Save rlafranchi/ee424d1d563d315e9176 to your computer and use it in GitHub Desktop.
class Matrix
attr_reader :rows, :columns
def initialize(str)
@rows = str.split("\n").map { |row| row.split(" ").map(&:to_i) }
@columns = @rows.transpose
end
def saddle_points
arr = []
# iterate through each row with it's index
# i is the row number
@rows.each_with_index do |row, i|
# find the maximum value in the row
row_max = row.max
# iterate through each value in the row
# k cooresponds to the appropriate column index
row.each_with_index do |row_value, k|
# find the minumum value in the column
column_min = @columns[k].min
# add the row and column index to the array
# if the value matches the min and the max
arr << [i,k] if row_value == column_min && row_value == row_max
end
end
arr
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment