Skip to content

Instantly share code, notes, and snippets.

@durrellchamorro
Last active March 25, 2016 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save durrellchamorro/50501e93b130db198d73 to your computer and use it in GitHub Desktop.
Save durrellchamorro/50501e93b130db198d73 to your computer and use it in GitHub Desktop.
Saddle Points
class Matrix
def initialize(raw_data_string)
@data = raw_data_string
end
def rows
format @data
end
def columns
rows.transpose
end
def saddle_points
marked_rows = mark_potential_row_saddle_points rows
organized_rows = organize_rows_for_column_matching(marked_rows).flatten
marked_columns = mark_potential_column_saddle_points(columns).flatten
potential_saddle_points = marked_columns.zip(organized_rows)
potential_saddle_points.reject { |coordinate| coordinate.include? nil }
end
private
def mark_potential_row_saddle_points(rows)
rows.map do |row|
highest_number = row.sort.last
row.each_index.map do |index|
index if row[index] >= highest_number
end
end
end
def mark_potential_column_saddle_points(columns)
columns.map do |column|
lowest_number = column.sort.first
column.each_index.map do |index|
index if column[index] <= lowest_number
end
end
end
def organize_rows_for_column_matching(marked_rows)
marked_rows.transpose
end
def format(raw_data_string)
raw_data_array = raw_data_string.split("\n")
raw_data_array.map { |number_string| number_string.split(" ").map(&:to_i) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment