Skip to content

Instantly share code, notes, and snippets.

@broguinn
Created August 30, 2013 01:00
Show Gist options
  • Save broguinn/6385246 to your computer and use it in GitHub Desktop.
Save broguinn/6385246 to your computer and use it in GitHub Desktop.
saddlepoints unfinished
require 'matrix'
class Matrix
def saddlepoints
saddles = []
self.each_with_index do |e, each_row, each_col|
if self.row(each_row).all? { |rcell| rcell <= e } && self.row(each_col).all? { |ccell| ccell >= e }
saddles << [each_row, each_col]
end
end
saddles
end
end
matrix = Matrix[ [9, 8, 7],[5, 3, 2],[6, 6, 7] ]
puts matrix
require 'rspec'
require 'saddlepoints'
describe 'saddlepoints' do
it 'returns saddlepoints' do
matrix = Matrix[[9, 8, 7], [5, 3, 2], [6, 6, 7]]
matrix.saddlepoints.should eq [[1, 0]]
end
it 'returns some more saddlepoints' do
matrix = Matrix[[4, 5, 4], [3, 5, 5], [1, 5, 4]]
matrix.saddlepoints.should eq [[0, 1], [1, 1], [2, 1]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment