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

# Neftali's solution

## to Saddle Points in the Delphi Pascal Track

Published at Feb 08 2021 · 0 comments
Instructions
Test suite
Solution

Detect saddle points in a matrix.

So say you have a matrix like so:

``````    1  2  3
|---------
1 | 9  8  7
2 | 5  3  2     <--- saddle point at column 1, row 2, with value 5
3 | 6  6  7
``````

It has a saddle point at column 1, row 2.

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.

## Testing

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

If Delphi is properly installed, and `*.dpr` file types have been associated with Delphi, then double clicking the supplied `*.dpr` file will start Delphi and load the exercise/project. `control + F9` is the keyboard shortcut to compile the project or pressing `F9` will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File` drop down menu.

### When Questions Come Up

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

### Submitting Exercises

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>` directory.

For example, if you're submitting `ubob.pas` for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`.

## 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 may request help from a mentor.

``````//******************************************************************
// Hint: You will be using TTuple in your solution.  TTuple is
// declared in this unit.  It will be necessary for you to add
// a uses statement in the interface section of uSaddlePoints.pas.
//******************************************************************

interface
uses
DUnitX.TestFramework;

const
CanonicalVersion = '1.5.0';

type

[TestFixture]
public
[Test]
//    [Ignore('Comment the "[Ignore]" statement to run the test')]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]

[Test]
[Ignore]
end;

TTuple<T1, T2> = record
private
fValue1: T1;
fValue2: T2;
public
constructor Create(aValue1: T1; aValue2: T2);
property Value1 : T1 read fValue1;
property Value2 : T2 read fValue2;
end;

implementation

constructor TTuple<T1, T2>.Create(aValue1: T1; aValue2: T2);
begin
fValue1 := aValue1;
fValue2 := aValue2;
end;

const
rows = 3;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 9; values[0,1] := 8; values[0,2] := 7;
values[1,0] := 5; values[1,1] := 3; values[1,2] := 2;
values[2,0] := 6; values[2,1] := 6; values[2,2] := 7;

SetLength(expected, 1);
expected[0] := TTuple<integer,integer>.Create(2,1);
end;

const
rows = 0;
columns = 0;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);

SetLength(expected, 0);
end;

const
rows = 4;
columns = 1;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 2;
values[1,0] := 1;
values[2,0] := 4;
values[3,0] := 1;

SetLength(expected, 2);
expected[0] := TTuple<integer,integer>.Create(2,1);
expected[1] := TTuple<integer,integer>.Create(4,1);
end;

const
rows = 1;
columns = 4;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 2; values[0,1] := 5; values[0,2] := 3; values[0,3] := 5;

SetLength(expected, 2);
expected[0] := TTuple<integer,integer>.Create(1,2);
expected[1] := TTuple<integer,integer>.Create(1,4);
end;

const
rows = 3;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 1; values[0,1] := 2; values[0,2] := 3;
values[1,0] := 3; values[1,1] := 1; values[1,2] := 2;
values[2,0] := 2; values[2,1] := 3; values[2,2] := 1;

SetLength(expected, 0);
end;

const
rows = 3;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 4; values[0,1] := 5; values[0,2] := 4;
values[1,0] := 3; values[1,1] := 5; values[1,2] := 5;
values[2,0] := 1; values[2,1] := 5; values[2,2] := 4;

SetLength(expected, 3);
expected[0] := TTuple<integer,integer>.Create(1,2);
expected[1] := TTuple<integer,integer>.Create(2,2);
expected[2] := TTuple<integer,integer>.Create(3,2);
end;

const
rows = 3;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 6; values[0,1] := 7; values[0,2] := 8;
values[1,0] := 5; values[1,1] := 5; values[1,2] := 5;
values[2,0] := 7; values[2,1] := 5; values[2,2] := 6;

SetLength(expected, 3);
expected[0] := TTuple<integer,integer>.Create(2,1);
expected[1] := TTuple<integer,integer>.Create(2,2);
expected[2] := TTuple<integer,integer>.Create(2,3);
end;

const
rows = 2;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 3; values[0,1] := 1; values[0,2] := 3;
values[1,0] := 3; values[1,1] := 2; values[1,2] := 4;

SetLength(expected, 2);
expected[0] := TTuple<integer,integer>.Create(1,1);
expected[1] := TTuple<integer,integer>.Create(1,3);
end;

const
rows = 3;
columns = 3;
var
values: TArray<TArray<integer>>;
expected: TArray<TTuple<integer,integer>>;
begin
SetLength(values, rows, columns);
values[0,0] := 8; values[0,1] := 7; values[0,2] := 9;
values[1,0] := 6; values[1,1] := 7; values[1,2] := 6;
values[2,0] := 3; values[2,1] := 2; values[2,2] := 5;

SetLength(expected, 1);
expected[0] := TTuple<integer,integer>.Create(3,3);
end;

initialization
end.``````
``````unit uSaddlePoints;

interface

uses

type
// Integer matrix
TMatrixInteger = record
private
Matrix:TArray<TArray<integer>>;
procedure Assign(ASource:TArray<TArray<integer>>);
function RowCount:integer;                             // number of rows
function ColCount:integer;                             // number of columns
function IsSaddleCol(ARow, ACol: integer): boolean;    // If is SaddlePoint with col values
function IsSaddleRow(ARow, ACol: integer): boolean;    // If is SaddlePoint with row values
end;

TArrayTuplas = TArray<TTuple<integer,integer>>;

function Calculate:TArrayTuplas;
end;

private
M:TMatrixInteger;
public
function Calculate:TArrayTuplas;
constructor Create(AMatrix:TArray<TArray<integer>>); reintroduce;
end;

implementation

uses
System.Sysutils;

begin
end;

var
row, col, count:Integer;
begin
count := 0;
for row := 0 to (M.RowCount-1) do begin
for col := 0 to (M.ColCount-1) do begin
SetLength(Result, count+1);
Result[count].Create(row+1, col+1);
Inc(count);
end;
end;
end;
end;

begin
M.Assign(AMatrix);
end;

begin
end;

procedure TMatrixInteger.Assign(ASource: TArray<TArray<integer>>);
var
row, col:Integer;
begin
SetLength(Matrix, Length(ASource));
for col := 0 to (Length(ASource)-1) do begin
SetLength(Matrix[col], Length(ASource[col]));
for row := 0 to (Length(ASource[col])-1) do
Matrix[col][row] := ASource[col][row];
end;
end;

function TMatrixInteger.RowCount: integer;
begin
Result := Length(Matrix);
end;

function TMatrixInteger.ColCount: integer;
begin
Result := 0;
if RowCount > 0 then
Result := Length(Matrix[0]);
end;

var
i:integer;
begin
Result := True;
for i := 0 to (RowCount - 1) do begin
if (Matrix[i][ACol] < Matrix[ARow, ACol]) then begin
Result := False;
Exit;
end;
end;
end;

var
i:integer;
begin
Result := True;
for i := 0 to (ColCount - 1) do begin
if (Matrix[ARow][i] > Matrix[ARow, ACol]) then begin
Result := False;
Exit;
end;
end;
end;

end.``````