Created
August 30, 2013 01:00
-
-
Save broguinn/6385246 to your computer and use it in GitHub Desktop.
saddlepoints unfinished
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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