ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at May 01 2021
·
0 comments

Instructions

Test suite

Solution

Detect saddle points in a matrix.

So say you have a matrix like so:

```
0 1 2
|---------
0 | 9 8 7
1 | 5 3 2 <--- saddle point at (1,0)
2 | 6 6 7
```

It has a saddle point at (1, 0).

It's called a "saddle point" because it is greater than or equal to every element in its row and less than or equal to every element in its column.

A matrix may have zero or more saddle points.

Your code should be able to provide the (possibly empty) list of all the saddle points for any given matrix.

The matrix can have a different number of rows and columns (Non square).

Note that you may find other definitions of matrix saddle points online, but the tests for this exercise follow the above unambiguous definition.

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

```
gem install minitest
```

If you would like color output, you can `require 'minitest/pride'`

in
the test file, or note the alternative instruction, below, for running
the test file.

Run the tests from the exercise directory using the following command:

```
ruby saddle_points_test.rb
```

To include color from the command line:

```
ruby -r minitest/pride saddle_points_test.rb
```

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

```
require 'minitest/autorun'
require_relative 'saddle_points'
class MatrixTest < Minitest::Test
def test_extract_a_row
matrix = Matrix.new("1 2\n10 20")
assert_equal [1, 2], matrix.rows[0]
end
def test_extract_same_row_again
skip
matrix = Matrix.new("9 7\n8 6")
assert_equal [9, 7], matrix.rows[0]
end
def test_extract_other_row
skip
matrix = Matrix.new("9 8 7\n19 18 17")
assert_equal [19, 18, 17], matrix.rows[1]
end
def test_extract_other_row_again
skip
matrix = Matrix.new("1 4 9\n16 25 36")
assert_equal [16, 25, 36], matrix.rows[1]
end
def test_extract_a_column
skip
matrix = Matrix.new("1 2 3\n4 5 6\n7 8 9\n 8 7 6")
assert_equal [1, 4, 7, 8], matrix.columns[0]
end
def test_extract_another_column
skip
matrix = Matrix.new("89 1903 3\n18 3 1\n9 4 800")
assert_equal [1903, 3, 4], matrix.columns[1]
end
def test_no_saddle_point
skip
matrix = Matrix.new("2 1\n1 2")
assert_equal [], matrix.saddle_points
end
def test_a_saddle_point
skip
matrix = Matrix.new("1 2\n3 4")
assert_equal [[0, 1]], matrix.saddle_points
end
def test_another_saddle_point
skip
matrix = Matrix.new("18 3 39 19 91\n38 10 8 77 320\n3 4 8 6 7")
assert_equal [[2, 2]], matrix.saddle_points
end
def test_multiple_saddle_points
skip
matrix = Matrix.new("4 5 4\n3 5 5\n1 5 4")
assert_equal [[0, 1], [1, 1], [2, 1]], matrix.saddle_points
end
end
```

```
=begin
Write your code for the 'Saddle Points' exercise in this file. Make the tests in
`saddle_points_test.rb` pass.
To get started with TDD, see the `README.md` file in your
`ruby/saddle-points` directory.
=end
class Matrix
def initialize(input)
@matrix = input.split("\n").map { |e| e.split.map(&:to_i) }
end
def rows
@matrix
end
def columns
@matrix.transpose
end
def saddle_points
res = []
for k in 0..rows.length - 1 do
max = rows[k].max
for i in 0..columns.length - 1 do
min = columns[i].min
res << [k,i] if @matrix[k][i] == max && @matrix[k][i] == min
end
end
return res
# res = []
# minCol = Hash.new
# for k in 0..@matrix.length-1 do
# for i in 0..@matrix[0].length-1 do
# min = min.nil? ? @matrix[i][k] : [min, @matrix[i][k]].min
# end
# minCol[k] = min
# min = nil;
# end
# for k in 0..@matrix.length-1 do
# max = @matrix[k].max
# for i in 0..@matrix[k].length-1 do
# if @matrix[k][i] == max && @matrix[k][i] == minCol[i].to_i
# res << @matrix[k][i]
# end
# end
# end
# return res
end
end
# matrix = [[9,8,7], [5,3,2], [6,6,7]]
# SaddlePoints.saddle_points(matrix)
matrix = Matrix.new("18 3 39 19 91\n38 10 8 77 320\n3 4 8 6 7")
puts matrix.saddle_points
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments