🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of florinb

florinb's solution

to Saddle Points in the Erlang Track

Published at Dec 22 2020 · 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.

Running tests

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

Questions?

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.

Source

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

Submitting Incomplete Solutions

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

saddle_points_tests.erl

%% 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]).

saddle_points(Matrix) ->
  MaxPoints = matrix_max(Matrix),
  SaddlePoints = lists:filter(fun(Point) -> validate_saddle(Matrix, Point) end,
                              MaxPoints ),
  lists:map(fun({_V, R, C}) -> {R, C} end ,
            SaddlePoints).

row_max(Row, RowIndex) ->
  row_max(Row, 0, RowIndex, []).

row_max([], _CIndex, _RIndex, Res) -> Res;
row_max([H|T], 0, RIndex, []) ->
  row_max(T, 1, RIndex, [{H, RIndex, 0}]);
row_max([H|T], CIndex, RIndex, [{V,_,_}|_Tr]=Res) when H == V ->
  row_max(T, CIndex+1, RIndex, Res ++ [{H, RIndex, CIndex}]);
row_max([H|T], CIndex, RIndex, [{V,_,_}|_Tr]) when H > V ->
  row_max(T, CIndex+1, RIndex, [{H, RIndex, CIndex}]);
row_max([_H|T], CIndex, RIndex, Res) ->
  row_max(T, CIndex+1, RIndex, Res).

matrix_max(Matrix) ->
  matrix_max(Matrix, 0, []).

matrix_max([], _Index, Res) -> Res;
matrix_max([H|T], Index, Res) ->
  R = row_max(H, Index),
  matrix_max(T, Index+1, Res ++ R).

validate_saddle(Matrix, Point) ->
  validate_saddle(Matrix, Point, true).

validate_saddle(_M, _P, false) -> false;
validate_saddle([], _P, Res) -> Res;
validate_saddle([H|T], {V, _R, C}=Point, true) ->
  StillSaddle = lists:nth(C+1, H) >= V,
  validate_saddle(T, Point, StillSaddle).

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?