Exercism v3 launches on Sept 1st 2021. Learn more! ๐๐๐

Published at Jan 22 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.

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3`

is used as it is the official build and
dependency management tool for erlang now. Please refer to the tracks installation
instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

```
$ rebar3 eunit
```

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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.

```
%% based on canonical data version 1.3.0
%% https://raw.githubusercontent.com/exercism/problem-specifications/master/exercises/saddle-points/canonical-data.json
-module(saddle_points_tests).
-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").
can_identify_single_saddle_point_test() ->
Input=[
[9, 8, 7],
[5, 3, 2],
[6, 6, 7]
],
Expected=[{1, 0}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_that_empty_matrix_has_no_saddle_points_test() ->
Input=[
[]
],
Expected=[],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_lack_of_saddle_points_when_there_are_none_test() ->
Input=[
[1, 2, 3],
[3, 1, 2],
[2, 3, 1]
],
Expected=[],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_multiple_saddle_points_in_a_column_test() ->
Input=[
[4, 5, 4],
[3, 5, 5],
[1, 5, 4]
],
Expected=[{0, 1}, {1, 1}, {2, 1}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_multiple_saddle_points_in_a_row_test() ->
Input=[
[6, 7, 8],
[5, 5, 5],
[7, 5, 6]
],
Expected=[{1, 0}, {1, 1}, {1, 2}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_saddle_point_in_bottom_right_corner_test() ->
Input=[
[8, 7, 9],
[6, 7, 6],
[3, 2, 5]
],
Expected=[{2, 2}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_saddle_points_in_a_non_square_matrix_test() ->
Input=[
[3, 1, 3],
[3, 2, 4]
],
Expected=[{0, 0}, {0, 2}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_that_saddle_points_in_a_single_column_matrix_are_those_with_the_minimum_value_test() ->
Input=[
[2],
[1],
[4],
[1]
],
Expected=[{1, 0}, {3, 0}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
can_identify_that_saddle_points_in_a_single_row_matrix_are_those_with_the_maximum_value_test() ->
Input=[
[2, 5, 3, 5]
],
Expected=[{0, 1}, {0, 3}],
?assertMatch(Expected, lists:sort(saddle_points:saddle_points(Input))).
```

```
-module(saddle_points).
-export([saddle_points/1, transpose/3]).
saddle_points([Head | _Tail] = Matrix) when length(Matrix) > 0 andalso length(Head) > 0 ->
Transposed = transpose(Matrix),
RowHigh =
sets:from_list(row_traverse_map(Matrix, fun(X) -> index_of(X, lists:max(X)) end)),
ColumnLow =
sets:from_list(
lists:map(fun({A, B}) -> {B, A} end,
row_traverse_map(Transposed, fun(X) -> index_of(X, lists:min(X)) end))),
lists:sort(intersection(RowHigh, ColumnLow));
saddle_points(_) ->
[].
intersection(A, B) ->
sets:to_list(
sets:intersection(A, B)).
row_traverse_map(List, Fn) ->
row_traverse_map(List, Fn, 0, []).
row_traverse_map([], _, _, Acc) ->
lists:flatten(
lists:reverse(Acc));
row_traverse_map([Head | Tail], Fn, RowNum, Acc) ->
row_traverse_map(Tail,
Fn,
RowNum + 1,
[lists:map(fun(X) -> {RowNum, X} end, Fn(Head)) | Acc]).
transpose([Head | _Tail] = Matrix) ->
RowLength = length(Head),
transpose(Matrix, RowLength, []).
transpose(_Matrix, 0, Acc) ->
Acc;
transpose(Matrix, RowLength, Acc) ->
transpose(Matrix,
RowLength - 1,
[lists:map(fun(Y) -> lists:nth(RowLength, Y) end, Matrix) | Acc]).
index_of(List, Item) ->
index_of(List, Item, [], 0).
index_of([], _, Acc, _) ->
Acc;
index_of([Item | Tail], Item, Acc, ColumnCounter) ->
index_of(Tail, Item, [ColumnCounter | Acc], ColumnCounter + 1);
index_of([_ | Tail], Item, Acc, ColumnCounter) ->
index_of(Tail, Item, Acc, ColumnCounter + 1).
```

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